这是我从中提取的数据示例:
+-----+------------+---------+----------+------------+
| 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 |
+-----+---------------------+
所以我的问题是,当我运行更新时,它只用第一行数据更新表,但我还需要用第二行附加它。
你应该group_concat连接的字符串,例如:
select id, group_concat(concat(date,'-' , carrier,'-', tracking))
from my_table
group by id
和更新
update my_update_table m
inner join (
select id, group_concat(concat(date,'-' , carrier,'-', tracking)) my_col
from my_table
group by id
) t on m.id = t.id
set m.shipping_info = t.my_col