gpt4 book ai didi

mysql - 使用sql中的SELECT和GROUP_CONCAT函数更新表行

转载 作者:行者123 更新时间:2023-11-29 13:55:24 24 4
gpt4 key购买 nike

我的 sql 数据库中有 3 个表。

这是我的表格,其中包含数据:

table1ID         WORD1          a2          b3          c7          o14         z32         q57         itable2ID         FORWORD1          apple1          orange3          disc13          disc23          disc314         book32         letter132         letter232         letter357         keyboard57         mousetable3ROWID      WORD       NAME1          a          NULL2          b          NULL3          c          NULL4          o          example, example15          z          NULL6          q          window, window17          i          NULL

table1 和 table2 具有相同的 ID。 table1 和 table3 有 WORD。我想更新 table3 中 NAME 列中的数据并得到如下结果:

table3ROWID      WORD       NAME1          a          apple, orange2          b          NULL3          c          disc1, disc2, disc34          o          NULL5          z          book6          q          letter1, letter2, letter37          i          mouse

请帮助解决该问题。

最佳答案

我认为您正在寻找带有 JOINUPDATE,使用 GROUP_CONCAT:

update table3 t3
join (
select t1.word, group_concat(t2.forward order by t2.forward separator ', ') name
from table1 t1
left join table2 t2 on t1.id = t2.id
group by t1.word
) t on t3.word = t.word
set t3.name = t.name;

SQL Fiddle Demo

请注意,第 7 行应返回键盘、鼠标,因为 table2 有 2 条记录,均为 57。

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

关于mysql - 使用sql中的SELECT和GROUP_CONCAT函数更新表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15939158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com