简体   繁体   English

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

[英]Return one row for a table Left join with another table has multirow with same ID

customers table: 客户表:

在此处输入图片说明

notes table: 备注表:

在此处输入图片说明

MY SQL query didn't work 我的SQL查询无效

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

I want to return one row for customer_id 277 : Mr,Kevin, New notes, Service, 2017-06-17 12:37:28 New note 2, Lead, 2017-06-17 15:04:42 我想为customer_id 277返回一行:Mr,Kevin,新记录,服务,2017-06-17 12:37:28新记录2,线索,2017-06-17 15:04:42

customers table left join with multi tables 客户表左连接多表

在此处输入图片说明

You could use a group_concat on formatted result for notes 您可以在格式化结果上使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加入1个表中具有单个ID /行但在另一个表中链接到多个行(相同ID)的SQL查询? - Join SQL query that has single id/row in 1 table, but links to multiple rows(same id) in another table? MySQL FULL JOIN一个表,并在同一个查询中LEFT JOIN另一个表? - MySQL FULL JOIN one table, and LEFT JOIN another in the same query? mysql左联接不返回所有左表行 - mysql left join not return all left table row 如何使用MySQL Left Join根据另一个表行的id获取结果? - How do I use MySQL Left Join to get results based on another table row's id? MySql LEFT连接多个具有相同ID或名称的表 - MySql LEFT join multiple table same with same id or name MySQL连接,从一个表返回1行,从另一个表返回多行作为数组或列表 - MySQL join, return 1 row from one table and multiple rows from another table as an array or list 如何选择(所有)表1中的一行,而另一行与表2相同 - How to select (everything) one row in table 1 which has another row that is the same as table 2 CodeIgniter两个表左连接在右表中未命中时不返回左表的连接条件id - CodeIgniter Two tables Left Join does not return Join Condition id of left table when miss in right table 从一个表中选择结果,并为每一行从另一张表中选择具有相同ID的所有行 - Select results from one table and for each row select all rows that have the same id from another table LEFT JOIN仅返回右表的一行 - LEFT JOIN only returns one row from right table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM