【问题标题】:If there is a way to achieve this kind of result below using group_concat如果有办法在下面使用 group_concat 实现这种结果
【发布时间】:2021-11-21 11:33:41
【问题描述】:

数据

Brcode  name
1.      A
2.      A
3.      A

预期结果

Brcode  Name Common
1        A             1,2,3
2        A              1,2,3
3        A              1,2,3
 

【问题讨论】:

  • 可能是子选择?
  • 对不起,我是新来的。我已经投票并标记为已接受您的答案。真的行。非常感谢先生。上帝保佑

标签: mysql group-concat


【解决方案1】:

是的,你可以加入。

CREATE TABLE test_tbl        (
             Brcode int(9),
              name varchar(3) );


    INSERT INTO test_tbl VALUES (1,'A'),
                                (2,'A'),
                                (3,'A');
    

SELECT t1.Brcode,
       t1.name,
       t2.common
FROM test_tbl t1       
inner join 
(
  select name ,group_concat(Brcode  SEPARATOR ',') as Common from test_tbl group by name
) as t2 on t1.name=t2.name;

演示:https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/99

结果:

【讨论】:

    猜你喜欢
    • 2014-06-12
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    相关资源
    最近更新 更多