且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何合并来自具有不同列的多个表的结果?

更新时间:2023-11-26 22:14:40

这应该可以正常工作:

SELECT * FROM `beards` b LEFT OUTER JOIN `mustaches` ON (0) WHERE  person = "bob"
UNION ALL
SELECT * FROM `beards` b RIGHT OUTER JOIN `mustaches` ON (0) WHERE  person = "bob"

您不必自己处理列.左右外部联接可以完成这项工作. 不幸的是,mysql没有完全连接.这就是为什么您必须通过工会这样做

you don't have to handle the columns by yourself. the left and right outer join do this job. unfortunately mysql doesn't have a full join. that's why you have to do it this way with a union

SELECT * FROM `customer` b LEFT OUTER JOIN `charges` ON (0) LEFT OUTER JOIN `day` ON (0)
UNION
SELECT * FROM `customer` b RIGHT OUTER JOIN `charges` ON (0) LEFT OUTER JOIN `day` ON (0)
UNION
SELECT * FROM `customer` b LEFT OUTER JOIN `charges` ON (0) RIGHT OUTER JOIN `day` ON (0)

这是我进行的本地测试