Sql join, 2 tables, same fields

前端 未结 3 704
你的背包
你的背包 2021-01-24 18:06

I have 2 tables. To simplify:

Table 1, users:


userId int, userName nvarchar(50)


Table 2 , messages:


msgId int, msgFrom int,

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-24 18:31

    You need to join onto tabUsers twice. One to get the sender, one to get the recipient:

    SELECT m.*, f.userName as Sender, t.userName as Recipient 
    FRPOM tabMessages AS m
        INNER JOIN tabUsers AS f on m.msgFrom = f.userId
        INNER JOIN tabUsers AS t on m.msgTo = t.userId 
    WHERE m.msgId = @someParameter;
    

提交回复
热议问题