Jump to content

[SOLVED] LEFT JOINS


esport

Recommended Posts

Hi guys, I have 2 tables 'tasks' and 'tasks_assigned_to'.

I want to display all the tasks in the 'tasks' table'.

 

easy

 

select * from tasks

 

however, when I join the 2 tables like

 

SELECT * FROM tasks t LEFT JOIN tasks_asigned_to ta ON t.ID=ta.task_id

 

It will only display the tasks that are in both tables. How do I display all tasks even if they aren't in the 'task_assigned_to' table.

 

Thanks

 

Daniel

Link to comment
https://forums.phpfreaks.com/topic/100236-solved-left-joins/
Share on other sites

A LEFT JOIN IS the answer to your question, as it will SELECT everything that is in the left-hand table and JOIN it to the right-hand table EVEN if there is no information pertaining to a JOIN in the right hand table. (tasks is left-hand table, and tasks_assigned_to is right-hand table).

 

A straight/inner JOIN will ONLY select data that corresponds in both tables.

Link to comment
https://forums.phpfreaks.com/topic/100236-solved-left-joins/#findComment-512751
Share on other sites

I relised why it wont produce the results I want. I have a GROUP BY clause in the query grouping the taskid's in the task_assigned_to table so it wont produce duplicates. If so, how would I produce a result with all tasks without duplicates?

 

SELECT * FROM tasks t LEFT JOIN tasks_asigned_to ta ON t.ID=ta.task_id GROUP BY ta.task_id

Link to comment
https://forums.phpfreaks.com/topic/100236-solved-left-joins/#findComment-513460
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.