select multiple rows in one result row

后端 未结 2 1947
暗喜
暗喜 2020-12-15 05:27

In one of my tables i store my advertisement data, thats one row per advertisement. I also store some dates in an other table, but that\'s one row per date because i don\'t

相关标签:
2条回答
  • 2020-12-15 05:43

    You can use GROUP_CONCAT() and GROUP BY to get the results you desire:

    SELECT t1.*, GROUP_CONCAT(t2.date) AS dates
    FROM Table1 t1
    LEFT JOIN Table2 t2
      ON t2.ID_adv = t1.ID_adv
    GROUP BY t1.ID_adv
    

    This returns all the dates for each advertisement, concatenated by commas. Where there are no dates in Table2 for a particular advertisment, you'll get NULL for the dates column.

    To target a particular advertisement, simply add the WHERE clause:

    SELECT t1.*, GROUP_CONCAT(t2.date) AS dates
    FROM Table1 t1
    LEFT JOIN Table2 t2
      ON t2.ID_adv = t1.ID_adv
    WHERE t1.ID_adv = 3
    GROUP BY t1.ID_adv
    
    0 讨论(0)
  • 2020-12-15 05:54

    You can turn rows into a column with GROUP_CONCAT function

    0 讨论(0)
提交回复
热议问题