How to combine results from multiple tables with different columns?

前端 未结 5 1899
北恋
北恋 2020-12-16 05:42

I have several tables with different numbers and types of columns, and a single column in common.

+--------+---------+------------+-------------+
| person |          


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 06:05

    this should be working fine:

    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"
    

    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)
    

    this is a local test i made

提交回复
热议问题