Full Outer Join (left join union right join) MySQL 多个表
Full Outer Join (left join union right join) MySQL multiple tables
我查看了其他相关的 SO 问题,但他们无法为我点击。
我有这些表:
http://i.stack.imgur.com/cEYVn.png
最后,我想合并这 5 个表,以便从 Location(可能 loc_type (int) 替换为 loc_type_name)到 loc_id 之外的所有内容营业时间(因为它将在 loc_id 加入),与假期相同,等等
我已经知道如何在 MySQL 中复制此功能:
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
我让它适用于任意两个表,但无法继续:
SELECT * FROM locations loc
LEFT OUTER JOIN location_types locType on locType.id = loc.loc_num
UNION
SELECT * FROM locations loc
RIGHT OUTER JOIN location_types locType on locType.id = loc.loc_num
UNION
SELECT * FROM locations loc
LEFT OUTER JOIN hours shours on loc.loc_num = shours.loc_id
UNION
SELECT * FROM locations loc
RIGHT OUTER JOIN hours shours on loc.loc_num = shours.loc_id
UNION
SELECT * FROM locations loc
LEFT OUTER JOIN holiday hol on loc.loc_num = hol.loc_id
UNION
SELECT * FROM locations loc
RIGHT OUTER JOIN holiday hol on loc.loc_num = hol.loc_id
任何帮助将不胜感激!
注意:位置和其他表格可能会有一些额外的列,所以最后,我想要一个我将在其中手动选择所需列的列(即从位置 select loc_id, loc_name, loc_phone)...
请看下面的查询,希望对您有所帮助!稍后您可以相应地修改
这将加入所有 5 个 table。在这里,我将位置保留为左侧 Table,以便在触发此查询后显示所有位置,并且匹配的 ID 将与此 table.
匹配
SELECT * FROM location LEFT JOIN location_type ON location_type.type_id=location.loc_id
LEFT JOIN hours ON location.loc_id=hours.loc_id
LEFT JOIN holiday ON holiday.loc_id=location.loc_id
LEFT JOIN holiday_hours ON holiday.hol_id=holiday_hours.holiday_id
我查看了其他相关的 SO 问题,但他们无法为我点击。
我有这些表: http://i.stack.imgur.com/cEYVn.png
最后,我想合并这 5 个表,以便从 Location(可能 loc_type (int) 替换为 loc_type_name)到 loc_id 之外的所有内容营业时间(因为它将在 loc_id 加入),与假期相同,等等
我已经知道如何在 MySQL 中复制此功能:
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
我让它适用于任意两个表,但无法继续:
SELECT * FROM locations loc
LEFT OUTER JOIN location_types locType on locType.id = loc.loc_num
UNION
SELECT * FROM locations loc
RIGHT OUTER JOIN location_types locType on locType.id = loc.loc_num
UNION
SELECT * FROM locations loc
LEFT OUTER JOIN hours shours on loc.loc_num = shours.loc_id
UNION
SELECT * FROM locations loc
RIGHT OUTER JOIN hours shours on loc.loc_num = shours.loc_id
UNION
SELECT * FROM locations loc
LEFT OUTER JOIN holiday hol on loc.loc_num = hol.loc_id
UNION
SELECT * FROM locations loc
RIGHT OUTER JOIN holiday hol on loc.loc_num = hol.loc_id
任何帮助将不胜感激!
注意:位置和其他表格可能会有一些额外的列,所以最后,我想要一个我将在其中手动选择所需列的列(即从位置 select loc_id, loc_name, loc_phone)...
请看下面的查询,希望对您有所帮助!稍后您可以相应地修改
这将加入所有 5 个 table。在这里,我将位置保留为左侧 Table,以便在触发此查询后显示所有位置,并且匹配的 ID 将与此 table.
匹配SELECT * FROM location LEFT JOIN location_type ON location_type.type_id=location.loc_id
LEFT JOIN hours ON location.loc_id=hours.loc_id
LEFT JOIN holiday ON holiday.loc_id=location.loc_id
LEFT JOIN holiday_hours ON holiday.hol_id=holiday_hours.holiday_id