从多个表中选择Mysql?

时间:2015-08-02 13:00:35

标签: mysql

我有3张桌子:

Table A1 : ID|Name|Date|Id_person
Table B1 : ID|Name|Date|Id_person
Table C1 : ID|Name|Date|Id_person

我想要一个类似的查询:

select * from table A1 as a , table B1 as b , table C1 as c where a.Id_person=1 or b.Id_person=1 or c.Id_person=1

只有在3个表中id_person = 1时才会得到结果, 如果只有一张表不符合条件,我就得不到结果。

如果其中一个主题不尊重条件(id_person = 1),如何从表中获取结果。

3 个答案:

答案 0 :(得分:1)

您可以尝试:

SELECT * FROM A1 WHERE Id_person=1 UNION SELECT * FROM A2 WHERE Id_person=1 UNION SELECT * FROM A3 WHERE Id_person=1

答案 1 :(得分:0)

试试:

select * from table A1 as a 
left join table B1 as b on a.Id_Person = b.Id_Person
left join table C1 as c on a.Id_Person = c.Id_Person

请参阅此帖子:LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

答案 2 :(得分:0)

您可以尝试UNION:

SELECT * FROM A1 as a WHERE a.Id_person=1
UNION ALL
SELECT * FROM B1 as b WHERE b.Id_person=1
UNION ALL
SELECT * FROM C1 as c WHERE c.Id_person=1