从几个表中选择all,其中一列匹配all

时间:2016-11-07 18:49:34

标签: mysql sql

我正在尝试使用php和mysql数据库做类似的事情:

SELECT * FROM accomodation,assessment,beneficiaries,site 
FROM accomodation,assessment,beneficiaries,site 
WHERE ITS_Code != "none"      

ITS_Code在所有表格之间是相互的 谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用加入

SELECT a.*, b.*, c.*, d.* 
FROM accomodation as  a
LEFT Join assessment as b on a.ITS_Code = b.ITS_Code 
LEFT JOIN beneficiaries as c on a.ITS_Code = c.ITS_Code 
LEFT JOIN site as d on a.ITS_Code = d.ITS_Code 
WHERE a.ITS_Code != "none"      

答案 1 :(得分:0)

假设表格住宿评估受益人网站具有相同数量的列,并且您我只是想连续列出每个表的内容,你可以这样做:

SELECT Column1, Column2, Column3 FROM accomodation
WHERE ITS_code <> "none"

UNION ALL

SELECT Column1, Column2, Column3 FROM assessment
WHERE ITS_code <> "none"

UNION ALL

SELECT Column1, Column2, Column3 FROM beneficiaries
WHERE ITS_code <> "none"

UNION ALL

SELECT Column1, Column2, Column3 FROM site
WHERE ITS_code <> "none"

如果其中一个表格的列数不等 - 例如网站只有2列 - 那么您可以SELECT NULL取代它,例如:

SELECT Column1, Column2, Column3 FROM accomodation
WHERE ITS_code <> "none"

UNION ALL

SELECT Column1, Column2, Column3 FROM assessment
WHERE ITS_code <> "none"

UNION ALL

SELECT Column1, Column2, Column3 FROM beneficiaries
WHERE ITS_code <> "none"

UNION ALL

SELECT Column1, Column2, NULL AS Column3 FROM site
WHERE ITS_code <> "none"