2017-08-16 173 views
-1

我的表如下分開:mysql如何GROUP_CONCAT結果在多行

create table testing(
    bnum varchar(7) 
); 

,這裏是值:

insert into testing values 
('0547366'), 
('0547367'), 
('0547368'), 
('0547369'), 
('0547370'), 
('0547371'), 
('0547372'), 
('0547373'), 
('0547374'), 
('0547375'), 
('0547376'); 

我用下面的查詢:

select group_concat(bnum) as nums from testing; 

並得到以下結果:

+============ 
nums 
+============ 
0547366,0547367,0547368,0547369,0547370,0547371,0547372,0547373,0547374,0547375,0547376 
+============ 

但我想有以下結果,我需要多組數字,其中的3個數字在每一組由逗號分隔:

+============ 
nums 
+============ 
0547366,0547367,0547368 
+============ 
0547369,0547370,0547371 
+============ 
0547372,0547373,0547374 
+============ 
0547375,0547376 
+============ 

什麼會查詢?請幫忙 !!!

+1

預期結果背後的邏輯是什麼? – Jens

+0

其實我的結果集太長了,我想分開每組中有3個數字的結果。我的實際表格更加複雜,所以我簡化了表格,以便每個人都能更好地理解我的問題。 – Tareq

+1

相應編輯你的問題 – Strawberry

回答

1
select GROUP_CONCAT(bnum),@cnt := @cnt+1,concat('a',@cnt1), 
    case when MOD(@cnt,3)=0 then @cnt1:[email protected]+1 end,@cnt1 
    from testing,(SELECT @cnt :=0,@cnt1:=0)z GROUP BY @CNT1 ORDER [email protected]; 

您可以嘗試以上查詢。

Fiddle Demo在這裏。

+1

非常感謝您的幫助。它的工作! – Tareq

+0

@Tareq保持它..! –

+0

我的電子郵件是[email protected] – Tareq