为表返回一行与另一个表的左连接具有相同ID的多行

时间:2017-06-17 19:10:52

标签: php mysql

客户表:

enter image description here

备注表:

enter image description here

我的SQL查询无效

SELECT * FROM customers LEFT JOIN notes  ON  customers.customer_id = notes.customer_id GROUP BY notes.customer_id 

我想为customer_id 277返回一行:  先生,凯文,新笔记,服务,2017-06-17 12:37:28            新笔记2,Lead,2017-06-17 15:04:42

客户表左连接多表

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在格式化结果上使用group_concat作为笔记

   select a.customer_id, a.ttle, a.first_name, group_concat(t.note_row)
   from customer
   inner join ( 
      select customer_id, concat(note, ' ', note_type, ' ', note_date_added ) as note_row
      from notes 
     ) t on t.customer_id = a.customer_id
    group by a.customer_id, a.ttle, a.first_name