简体   繁体   English

MySQL GROUP_CONCAT 与 COLUMN SPLIT

[英]MySQL GROUP_CONCAT with COLUMN SPLIT

I am working with a TABLE, need logical help.我正在使用 TABLE,需要逻辑帮助。

Check the below URL for the table structure and sample data.检查以下 URL 以获取表结构和示例数据。

http://sqlfiddle.com/#!2/ece06/2 http://sqlfiddle.com/#!2/ece06/2

Table Schema:表架构:

CREATE TABLE test (
  ID INTEGER,
  NAME VARCHAR (50),
  VALUE INTEGER
);

Inserted Data:插入的数据:

INSERT INTO test VALUES (1, 'A', 4);
INSERT INTO test VALUES (1, 'B', 5);
INSERT INTO test VALUES (1, 'C', 8);
INSERT INTO test VALUES (2, 'D', 9);
INSERT INTO test VALUES (2, 'E', 9);
INSERT INTO test VALUES (3, 'F', 9);
INSERT INTO test VALUES (3, 'G', 9);
INSERT INTO test VALUES (3, 'H', 9);
INSERT INTO test VALUES (3, 'I', 9);

Query:查询:

SELECT ID, GROUP_CONCAT(NAME) AS CODE
FROM test
GROUP BY ID;

OutPUT:输出:

ID  CODE
1   A,B,C
2   D,E
3   F,G,H,I

Expected OUTPUT:预期输出:

ID  CODE   CODE   CODE  CODE
1    A      B      C    NULL
2    D      E     NULL  NULL
3    F      G      H     I

As you can see the output of the query has concat with comma.如您所见,查询的输出使用逗号连接。 And currently we are doing string concat using PHP which splits while displaying!!目前我们正在使用 PHP 进行字符串连接,它在显示时拆分!!

Is there any other way to split the RESULT and show each value in column and same ROW?有没有其他方法可以拆分结果并在列和同一行中显示每个值? In same result?结果一样吗?

Note: The CODE might vary for each ROW.注意:每个 ROW 的 CODE 可能不同。

If you know the number of GROUP_CONCAT entries(I mean 3 fields are combined in the case of ID =1 and 2 fields are combined in the case of 2 etc), then there is a dirty way.如果您知道 GROUP_CONCAT 条目的数量(我的意思是在 ID = 1 的情况下组合了 3 个字段,在 2 等情况下组合了 2 个字段),那么有一种肮脏的方式。

SELECT ID, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(NAME), ',', 1), ',', -1) AS CODE1,
If(  length(GROUP_CONCAT(NAME)) - length(replace(GROUP_CONCAT(NAME), ',', ''))>1,  
       SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(NAME), ',', 2), ',', -1) ,NULL) 
           as CODE2,
   SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(NAME), ',', 3), ',', -1) AS CODE3
FROM test
GROUP BY ID;

Output:输出:

ID  CODE1   CODE2   CODE3
1   A          B    C
2   D       (null)  E
3   F          G    H

The above query assume that you are GROUP_CONCAT-ing 3 fields.上面的查询假设您是 GROUP_CONCAT-ing 3 个字段。 If you are dynamically generating query you can give a try.如果您正在动态生成查询,您可以尝试一下。 SQLFIDDLE SQLFIDDLE

EDIT: Note: The CODE might vary for each ROW.编辑:注意:每个行的代码可能会有所不同。 ( Ignoring this) (忽略这一点)

With Help from my colleague We arrived at a point to solve this senario.在我同事的帮助下,我们到了解决这个场景的地步。 Hope some one might needed it.. It is welcome if some one make it much simpler.希望有人可能需要它.. 如果有人让它更简单,欢迎。

BEGIN
SET @v=0;


SET @v1=0;


SELECT tmp.cnt INTO @v
FROM
  (SELECT Id,
          count(ID) AS cnt,
          GROUP_CONCAT(name)
   FROM test
   GROUP BY id) tmp
ORDER BY tmp.cnt DESC LIMIT 1;


SET @str=' ';

 WHILE(@v>@v1) DO
SET @v1=@v1+1;

 IF(@str='') THEN
SET @str=CONCAT(@str,'ID, REPLACE(SUBSTRING(SUBSTRING_INDEX(GROUP_CONCAT(NAME), '','',', @v1,'),LENGTH(SUBSTRING_INDEX(GROUP_CONCAT(NAME),'','',', @v1,'-1)) + 1),'','','''') AS Code' ,@v1);

 ELSE
SET @str= CONCAT(@str,',REPLACE(SUBSTRING(SUBSTRING_INDEX(GROUP_CONCAT(NAME),'','',', @v1,'),LENGTH(SUBSTRING_INDEX(GROUP_CONCAT(NAME),'','',' , @v1,' -1)) + 1),'','','''') AS Code',@v1);

 END IF;

 END WHILE;

SET @str=CONCAT('SELECT ' , @str, ' FROM test GROUP BY ID');

 PREPARE MYSQLQUERY
FROM @str;

 EXECUTE MYSQLQUERY;

 DEALLOCATE PREPARE MYSQLQUERY;

 END

The only way to split that into columns will mean that you know the values you want to split in advance (eg: one column for codes matching A, D, F another one for B, E, G and so on).将其拆分为列的唯一方法意味着您事先知道要拆分的值(例如:一列用于匹配A, D, F代码A, D, F另一列用于B, E, G等)。 This doesn't seem to be the case.情况似乎并非如此。

The bullet proof approach would be not to group by and process the results in PHP.防弹方法不是在 PHP 中分组和处理结果。

The other possible way (not the one I would recommend) would be to change the saparator from a comma to something else.另一种可能的方法(不是我推荐的方法)是将分隔符从逗号更改为其他内容。 EG: EG:

SELECT ID, GROUP_CONCAT(NAME SEPARATOR ';') AS CODE
FROM test
GROUP BY ID
(SELECT ID, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(NAME), ',', 1), ',', -1) AS CODE1,

If(  length(GROUP_CONCAT(NAME)) - length(replace(GROUP_CONCAT(NAME), ',', ''))>1,  

       SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(NAME), ',', 2), ',', -1) ,NULL)as CODE2,

   SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(NAME), ',', 3), ',', -1) AS CODE3,

       If(  length(GROUP_CONCAT(NAME)) - length(replace(GROUP_CONCAT(NAME), ',', ''))>2,  

       SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(NAME), ',', 4), ',', -1) ,NULL)as CODE4

FROM test
GROUP BY ID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM