我的第一个结果是这样的
ID Name
----------------------------------
1 George
2 Peter
3 San
我的另一个结果是这样的
AnotherID ID Note
-----------------------------------
1 1 georgesnote1
2 1 georgesnote2
3 3 sansnote1
4 1 georgesnote3
我希望它们看起来如何:
ID Name Note
----------------------------------
1 George georgesnote1
1 George georgesnote2
1 George georgesnote3
2 Peter NULL
3 San sansnote1
我的 SQL 知识在很大程度上限制了我实现这一点。我想我需要像 UNION ALL 这样的东西。 INNER JOIN 或 LEFT OUTER JOIN 不起作用。我的实际查询大约有 21 行,所以这不是初学者问题。我需要的是根据相同的 ID 加入两个结果。请有人指导我。
最佳答案
您需要一个 LEFT JOIN。
查看 http://www.w3schools.com/sql/sql_join_left.asp
假设第一个表称为first,第二个表称为second。 第一个表中的列 id 将与第二个表中的列 id 匹配。
SELECT first.id, first.name, second.note
FROM first
LEFT JOIN second
ON first.id = second.id
ORDER BY first.id
关于sql - 加入两个sql结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12929191/