OKay, I will try to make this make sense lol. Basically I have 4 different tables I am trying to join instead of doing a few separate SQL statements, as the MYSQL engine should be able to optimize it better than I could.
Basically I have these 4 tables:
projects (key:id)
project_users (key:project_id , user_id)
users (key:id)
tasks (key:project_id)
This is my query:
SELECT projects.*, users.*, tasks.*,
FROM
projects JOIN ( project_users JOIN
users ON
project_users.user = users.id )
ON projects.id = project_users.project_id
JOIN ( tasks )
ON projects.id = tasks.project_id
WHERE projects.company = 2 ORDER BY projects.id
Basically I am trying to get a list of projects, users in that project, and tasks in that project for a certain company.
THE ISSUE - If there is not a task in this project, it doesnt return that project. Which I still need that project, just with 0 tasks for that project.
I am new to JOIN statements, so if there is some improving I can do, PLEASE let me know!
THANK YOU FOR YOUR TIME AND HELP!!!!!
All you need to do is add left outer
before the join that joins the tasks. This will get you projects even though there are no tasks.
left or right joins are your new friends
http://dev.mysql.com/doc/refman/5.0/en/left-join-optimization.html