简体   繁体   中英

Select from multiple tables Mysql?

I have 3 tables :

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

I want a query like:

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

I get result only if id_person=1 in the 3 tables, And i get no result if just one the 3 table don't respect the condition.

How to get result from the tables even if one of theme don't respect the condition (id_person=1).

你可以试试:

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

Try that :

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

See this post : LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

You can try 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM