Search code examples
mysqlgroup-concat

MySQL Join from multiple tables with GROUP_CONCAT


I am trying to select data from 3 separate tables, where 2 of those contains multiple rows of data. The sql query i am using is this, but i am experiencing that when i run it, if either taskdependees or tasksdependencies have zero result, the whole task is not showing.

SELECT t.*, GROUP_CONCAT(a.DependenciesId) as DiesId, GROUP_CONCAT(b.DependeesId) as DeesId FROM tasks t JOIN tasksdependencies a ON a.TasksId=t.TasksId JOIN taskdependees b ON b.TasksId=t.TasksId GROUP BY t.TasksId

What am I doing wrong in this query?


Solution

  • Use LEFT JOIN , inner join will give row if there is association is present in both tables while left will return the rows from left table even if they are not associated

    SELECT t.*, GROUP_CONCAT(a.DependenciesId) as DiesId,
     GROUP_CONCAT(b.DependeesId) as DeesId 
    FROM tasks t
    LEFT JOIN tasksdependencies a ON a.TasksId=t.TasksId
    LEFT JOIN taskdependees b ON b.TasksId=t.TasksId 
    GROUP BY t.TasksId