我希望你能帮助我:我正在开发php / mysql并陷入这个sql问题 . 我有3个表,其中包含一些字段:Table1,Table2和Table3.Let put en example:
表/字段Table1.Id Table2.Id Table3.Id
因此,我需要在一个查询(SELECT)中获取Table2中存在于Table2或Table3中的行 . 实际上,我在两个查询中获取行并混合它(Table1.id = Table2.id)(Table1.id = Table3.id)
select * from table1 inner join table2 on table1.id=table2.id union select * from table1 inner join table3 on table1.id=table3.id
子查询可能会更好
select * from Table1 t1 where t1.id in (select t2.id from Table2 t2) or t1.id in (select t3.id from Table3 t3)
要么
select * from Table1 t1 where t1.id in (select t2.id from Table2 t2 union select t3.id from Table3 t3)
2 回答
子查询可能会更好
要么