How to intersect three tables with condition using MySQL?
I had 3 tables
1.usertb
email funcarea
2.professiontb
email experience
3.generalinfo
email location
I want to combine these three tables using the field email with 3 conditions like funarea = 2 and experience 开发者_Go百科>=3 and location = 'Sydney'
Since I'm a beginner to SQL, I am eager to know how to implement this.
select a.email,funarea,experience,location from usertb a
left join professiontb b
on a.email = b.email
left join generalinfo c
on a.email = c.email
where
funarea = 2 and experience >=3 and location = 'Sydney'
If you have at least one entry in each table:
(blunt and stupid but it works)
SELECT *
FROM usertb, professiontb, generalinfo
WHERE usertb.email=professiontb.email AND professiontb.email=generalinfo.email AND usertb.funcarea=2 AND professiontb.experience>=3 AND generalinfo.location='Sydney';
If you don't have one entry in each table, my advice would be to use the LEFT JOIN statement.
SELECT * FROM usertb WHERE usertb.funcarea=2
LEFT JOIN professiontb ON(usertb.email=professiontb.email AND professiontb.experience>=3)
LEFT JOIN generalinfo ON(usertb.email=generalinfo.email AND generalinfo.location='Sydney');
As is, you should get all the rows from usertb, even if there is some missing ones in the other tables.
精彩评论