MySQL使用concat更新并附加多行数据
下面是我从中提取的数据示例:MySQL使用concat更新并附加多行数据,mysql,Mysql,下面是我从中提取的数据示例: +-----+------------+---------+----------+------------+ | id | name | carrier | tracking | date | +-----+------------+---------+----------+------------+ | 123 | john smith | UPS | abcdefgh | 2018-06-22 | | 123 | john sm
+-----+------------+---------+----------+------------+
| 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 |
+-----+---------------------+
因此,我的问题是,当我运行更新时,它只使用第一行数据更新表,但我还需要将其附加到第二行。您应该将连接的字符串分组,例如:
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
表示组函数的使用无效。我试着在一个
小组内做一个concat
,结果得到了同样的错误你看了例子吗?是的。我知道在Select中,group\u concat
是如何工作的,但在我的情况下,它在更新和设置中会工作吗?在更新查询中如何工作?现在,我正在做一个更新,并加入了其他4个表(尽管我在最初的帖子中没有提到)。非常感谢。