如何在用于联接其他表以获取其他数据的第一列表中中断数据

时间:2019-01-23 08:10:18

标签: mysql sql

如何在用于联接其他表以获取其他数据的第一列表中中断数据。重点是 从代码与表1相同的数据分数列表2的结果中更改表数据

示例

tabel 1

no       description           code 
1          one                  A
1          two                  B
2          three                C

tabel 2

no         code              codeall
1          ABB                null
1          BBC                null
2          ABC                null

我想成为这样

tabel 2

no            code                  codeall
1               ABB                one, two, two
1               BBC                two, two,three
2               ABC                one,two,three

我如何修改此查询

UPDATE tabel1 tb1 JOIN tabel2 tb2 ON tb1.no = tb2.no 
SET tb2.codeall = ... where tb2.no = 1

3 个答案:

答案 0 :(得分:0)

您可以使用GROUP_CONCAT这样的东西

serialVersionUID

答案 1 :(得分:0)

在进行JOIN之前,您需要先汇总

UPDATE tabel2 tb2 JOIN
       (SELECT tb1.no, GROUP_CONCAT(description) as codeall
        FROM tabel1 tb1
        GROUP BY tb1.no
       ) tb1
       ON tb1.no = tb2.no 
    SET tb2.codeall = tb1.codeall
    WHERE tb2.no = 1;

为了提高性能,您应该在聚合之前进行过滤:

UPDATE tabel2 tb2 JOIN
       (SELECT tb1.no, GROUP_CONCAT(description) as codeall
        FROM tabel1 tb1
        WHERE tb1.no = 1
        GROUP BY tb1.no
       ) tb1
       ON tb1.no = tb2.no 
    SET tb2.codeall = tb1.codeall
    WHERE tb2.no = 1;

答案 2 :(得分:0)

tabel 1          
 no     description           code 
 1          39                  4
 1          64                  S
 1          70                  Z

tabel 2          
no        code           codeall
1          4SZ            null

become
tabel 2          
no            code                  codeall
1              4SZ             68,69,70,48,67,66,65 it should (39,64,70)

我尝试查询