issue with GROUP_CONCAT in JOIN query mysql

后端 未结 2 1895
长情又很酷
长情又很酷 2021-01-28 17:29

I have a join query where I left join 2 tables say tab_sectorand tab_sector_subdivisions. I have records in first table which may or may not have corre

相关标签:
2条回答
  • 2021-01-28 18:05

    group by missed that`s why it is returning one record,

      SELECT tab_sector.sector_id,tab_sector.sector_title,tab_sector.sector_desc,tab_sector.sector_image,GROUP_CONCAT(tab_sector_subdivisions.subdiv_id) 
    FROM tab_sector 
    LEFT JOIN tab_sector_subdivisions 
    ON tab_sector_subdivisions.sector_id = tab_sector.sector_id WHERE tab_sector.active = 'Y'
    group by tab_sector.sector_id,tab_sector.sector_title,tab_sector.sector_desc,tab_sector.sector_image;
    
    0 讨论(0)
  • 2021-01-28 18:15

    You need the group by for the not aggregated columns

    SELECT tab_sector.sector_id,tab_sector.sector_title,tab_sector.sector_desc
        ,tab_sector.sector_image,group_concat(tab_sector_subdivisions.subdiv_id )
    LEFT JOIN tab_sector_subdivisions 
            ON tab_sector_subdivisions.sector_id = tab_sector.sector_id 
                  AND tab_sector.active = 'Y'
    GROUP BY  tab_sector.sector_id,tab_sector.sector_title,
         tab_sector.sector_desc, tab_sector.sector_image
    
    0 讨论(0)
提交回复
热议问题