0

我需要帮助构建此查询,我需要从同一个表中选择不同的值但不同的唯一键。

为了详细说明,请提供以下示例:

我有 2 张桌子:

问题(IssueID、AuthorID_FK、AssigedID_FK、...)
用户(UserID、User_Label、...)

AuthorID_FK和AssigedID_FK链接到表用户,我需要在相同的查询结果中获取两者的User_Label

你能帮忙吗?

谢谢,

4

4 回答 4

1
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 中工作

于 2012-04-10T09:23:05.313 回答
0

好吧,这个也应该工作:)

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
于 2012-04-10T09:24:27.183 回答
0

惯于

SELECT a.IssueID, b.UserID 
FROM Issues a 
    JOIN Users b ON (a.AuthorID_FK=b.UserID OR a.AssignedID_FK = b.UserID)

工作?

于 2012-04-10T09:24:48.313 回答
0

您可能想尝试这样的事情

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
于 2012-04-10T09:27:18.790 回答