我有2个表。为了简化:Sql join,2表,相同字段
表1中,用户:
用户id INT, 的userName为nvarchar(50)
表2中,消息:
的msgId INT, msgFrom int, msgTo int ...
msg1和msg2都包含userId。现在我想获取所有消息,但不是msgFrom我想要用户名。我知道该在这里做什么:
select tabMessages.*, tabUsers.userName as Sender
from tabMessages
inner join tabUsers on msgFrom=userId
where msgId = @someParameter;
一切工作正常和丹迪。获取用户名而不是msgTo也是一样。现在的问题是,我该如何在同一个电话中获得BOTH字段?我想让表格成为
msgId,msgFrom,msgTo,Sender,Recipient。我试图:
select tabMessages.*, tabUsers.userName as Sender,
tabUsers.userName as Recipient
from tabMessages
inner join tabUsers on msgFrom=userId and msgTo=userId
where msgId = @someParameter;
但这并不起作用。我正在使用Ms sql2000。
+1。但请格式化您的SQL,以便在不必水平滚动的情况下查看它。 – bernie 2010-06-05 20:35:49
谢谢,会做:) – Anpher 2010-06-05 20:41:04