Table1: ID,Name,some more columns
Table2: ID
Table3: Name
我想从表 1 中获取输出,其 ID 存在于 Table2.IDs 中,其名称存在于 Table3.Name 中。
换句话说,选择所有 3 个表中都存在的数据。
例如:
表 1:
1 John
2 Will
3 Michael
表 2:
1
表 3:
Will
输出应该是
1 John
2 Will
最佳答案
您需要使用 JOIN。
您的描述和示例输出不匹配,所以我将分别给您一个示例。
根据你的描述,应该是2个INNER JOIN:
select table1.id, table1.name
from table1
inner join table2 on table2.id = table1.id
inner join table3 on table3.name = table1.name
根据您的输出,它应该是 2 个带有 WHERE 子句的 OUTER JOINS,指定至少满足 2 个连接之一:
select table1.id, table1.name
from table1
left outer join table2 on table2.id = table1.id
left outer join table3 on table3.name = table1.name
where table2.id is not null or table3.name is not null
关于mysql - 合并多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5317246/