我需要帮助构建此查询,我需要从同一个表中选择不同的值但不同的唯一键。
为了详细说明,请提供以下示例:
我有 2 张桌子:
问题(IssueID、AuthorID_FK、AssigedID_FK、...)
用户(UserID、User_Label、...)
AuthorID_FK和AssigedID_FK都链接到表用户,我需要在相同的查询结果中获取两者的User_Label。
你能帮忙吗?
谢谢,
我需要帮助构建此查询,我需要从同一个表中选择不同的值但不同的唯一键。
为了详细说明,请提供以下示例:
我有 2 张桌子:
问题(IssueID、AuthorID_FK、AssigedID_FK、...)
用户(UserID、User_Label、...)
AuthorID_FK和AssigedID_FK都链接到表用户,我需要在相同的查询结果中获取两者的User_Label。
你能帮忙吗?
谢谢,
SELECT a.IssueID, b.User_Label, c.User_Label FROM Issues a
INNER JOIN USERS b on a.AuthorID_FK = b.UserID
INNER JOIN USERS c on a.AssignedID_FK = c.UserID
类似的东西 :) 这应该在 MS SQL Server 中工作
好吧,这个也应该工作:)
SELECT IssueID, U.User_Label FROM Issues I
INNER JOIN Users U ON U.UserID = I.AuthorID_FK
UNION
SELECT IssueID, U.User_Label FROM Issues I
INNER JOIN Users U ON U.UserID = I.AssigedID_FK
惯于
SELECT a.IssueID, b.UserID
FROM Issues a
JOIN Users b ON (a.AuthorID_FK=b.UserID OR a.AssignedID_FK = b.UserID)
工作?
您可能想尝试这样的事情
SELECT
issues.IssueID,
Authour.User_Label AS Author_Label,
Assigned.User_Label AS Assigned_user_Label
FROM
issues
INNER JOIN users AS Authour ON Authour.UserID = issues.AuthorID_FK
INNER JOIN users AS Assigned ON Assigned.UserID = issues.AssignedID_FK