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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.