以下是我从中提取的数据示例:
+-----+------------+---------+----------+------------+
| id | name | carrier | tracking | date |
+-----+------------+---------+----------+------------+
| 123 | john smith | UPS | abcdefgh | 2018-06-22 |
| 123 | john smith | USPS | 12345678 | 2018-06-23 |
+-----+------------+---------+----------+------------+
我要更新的表的每个ID只有一个记录(而我从中提取的表可以有多个),而我试图使最终输出看起来像这样:
+-----+------------------------------------------+
| id | shipping_info |
+-----+------------------------------------------|
| 123 | 6/22 - UPS abcdefgh 6/23 - USPS 12345678 |
+-----+------------------------------------------+
我有一个查询,我快到了:
update table1
set shipping_info = concat(
DATE_FORMAT(table2.date_time, '\n%c/%e - '),
table2.carrier,
table2.tracking)
where id = '123'
+-----+---------------------+
| id | shipping_info |
+-----+---------------------|
| 123 | 6/22 - UPS abcdefgh |
+-----+---------------------+
所以我的问题是,当我运行更新时,它仅使用第一行数据更新表,但是我还需要在第二行中附加它。
答案 0 :(得分:1)
您应该group_concat连接字符串,例如:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
并进行更新
select id, group_concat(concat(date,'-' , carrier,'-', tracking))
from my_table
group by id