我知道我在问一些看起来微不足道的东西,但我无法找到并回答或重复。 (我是这个领域的新手)。 所以我在努力。
我有两张桌子 使用以下列测试表
1. storyID , authorID , assigneeID
2. userID , userName
我想写一个返回storyID, UserName of author ID, UserName of assigneeID
。
答案 0 :(得分:0)
SELECT storyID, u1.userName AS authorName, u2.userName AS assigneeName
FROM storytable s
INNER JOIN usertable u1 ON s.authorID = u1.userID
INNER JOIN usertable u2 ON s.assigneeID = u2.userID
只有在userID,assigneeID和authorID字段之间存在关联时,上述解决方案才有效。
答案 1 :(得分:0)
选择并为第二个表创建别名。
SELECT a.storyID, b.userName, c.userName
FROM tablea a
INNER JOIN tableb b ON a.authorID = b.userID
INNER JOIN tableb c ON a.assigneeID= c.userID
答案 2 :(得分:0)
假设authorID = UserId ...
select
1.storyId,
author.userName,
assignee.userName
from 1
inner join 2 as author
on 1.authorId = 2.userId
inner join 2 as assignee
on 1.assigneeID = 2.userID