0

是否可以INNER JOIN通过 MySQL 查询来实现此结果?

我有一张包含策略的表格和一张包含成员的表格。Strategy 表包含与 Member 表中的 ID 对应的作者 ID,以及更新现有作者作品的作者 ID。是否可以同时获取对这两个人的引用?类似于以下内容,它不返回错误,但也没有结果......

SELECT * FROM Strategies 
INNER JOIN Members AS a 
INNER JOIN Members AS b 
WHERE Strategies.ID='2' 
AND Strategies.AuthorID = a.ID 
AND Strategies.UpdateAuthorID = b.ID
4

2 回答 2

6

使用LEFT JOIN

SELECT 
  s.*,
  a.Name AS MemberName,
  b.Name AS UpdatedMemberName
FROM Strategies  AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2 
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2 ;

如果您希望它们在一列中使用COALESCE

SELECT 
  s.*,
  COALESCE(a.Name, b.Name) AS MemberName
FROM Strategies  AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2 
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2  
于 2013-03-05T06:59:34.897 回答
5
SELECT toD.dom_url AS ToURL, 
    fromD.dom_url AS FromUrl, 
    rvw.*

FROM reviews AS rvw

LEFT JOIN domain AS toD 
    ON toD.Dom_ID = rvw.rev_dom_for

LEFT JOIN domain AS fromD 
    ON fromD.Dom_ID = rvw.rev_dom_from

如果域是表名

于 2013-03-05T06:59:49.557 回答