Help with MySQL table job

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Help with MySQL table job

Post by Mr Tech »

First of all here is my MySQL. I've cut it down a little bit for easy reading:

Code: Select all

SELECT * FROM leads_tasks AS t 
JOIN leads AS l ON t.lead_id = l.id 
JOIN proposals_sent AS p ON t.database_id = p.proposal_id 
WHERE ....
Basically I need to join the first two tables: leads_tasks and leads. The third table (proposals_sent) is optional dependent on if there is any data from it. I only want to join it if there is anything that matches the t.database_id to p.proposal_id. If not, I just want it to join the first two tables and pull results from them.

However, with my current code, if there is nothing in the proposals_sent table, it says there are no rows.

I think I have to use LEFT JOIN or something like that but I'm not sure. Any help would be appreciated :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Help with MySQL table job

Post by VladSun »

Mr Tech wrote:I think I have to use LEFT JOIN or something like that but I'm not sure. Any help would be appreciated :)
Yes, you are right - use LEFT JOIN for proposals_sent table.

//offtopic
:twisted: :twisted: :twisted:
Avatars war !!!
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply