2013-01-05 53 views
4

我有一個MySQL表看起來像:如何轉置MySQL行並重複列標題?

id group_id item_code item_label item_detail item_score 
1 10   BLU123  Blue 123 Blah blah 123 3 
2 10   BLU124  Blue 124 Blah blah 124 6 
3 10   BLU125  Blue 125 Blah blah 125 2 

是否有任何SQL語句將輸出表:

group_id item_code1 item_label1 item_detail1 item_score1 item_code2 item_label2 item_detail2 item_score2 item_code3 item_label3 item_detail3 item_score3 
10   BLU123  Blue 123  Blah blah 123 3   BLU124  Blue 124  Blah blah 124 6   BLU125  Blue 125  Blah blah 125 2 

謝謝大家!

+1

世界爲什麼你要這麼做?無論如何,您可能需要在您的應用程序中編寫代碼,而不是使用SQL查詢來完成它。 – JohnFx

+1

迷人的是,你可以降級,因爲你*不知道我的推理。想知道是否可以這樣做,Excel組件可以在1個步驟中填充。 – user1950195

+0

也許「重複列標題」是誤導性的。如果列標題爲'id group_id item_code1 item_label1 item_detail1 item_score1 item_code2 item_label2 item_detail2 item_score2' – user1950195

回答

1

你可以做這樣的事情,如果這些真的是id值:

select group_id, 
    max(case when id = 1 then item_code end) item_code1, 
    max(case when id = 1 then item_label end) item_label1, 
    max(case when id = 1 then item_detail end) iitem_detail1, 
    max(case when id = 1 then item_score end) item_score1, 
    max(case when id = 2 then item_code end) item_code2, 
    max(case when id = 2 then item_label end) item_label2, 
    max(case when id = 2 then item_detail end) iitem_detail2, 
    max(case when id = 2 then item_score end) item_score2, 
    max(case when id = 3 then item_code end) item_code3, 
    max(case when id = 3 then item_label end) item_label3, 
    max(case when id = 3 then item_detail end) iitem_detail3, 
    max(case when id = 3 then item_score end) item_score3 
from yourtable 
group by group_id 

SQL Fiddle with Demo

結果:

| GROUP_ID | ITEM_CODE1 | ITEM_LABEL1 | IITEM_DETAIL1 | ITEM_SCORE1 | ITEM_CODE2 | ITEM_LABEL2 | IITEM_DETAIL2 | ITEM_SCORE2 | ITEM_CODE3 | ITEM_LABEL3 | IITEM_DETAIL3 | ITEM_SCORE3 | 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
|  10 |  BLU123 | Blue 123 | Blah blah 123 |   3 |  BLU124 | Blue 124 | Blah blah 124 |   6 |  BLU125 | Blue 125 | Blah blah 125 |   2 | 

如果你不能在表中依靠id ,那麼你可以添加一個行號給你正在返回的記錄:

select group_id, 
    max(case when rownum = 1 then item_code end) item_code1, 
    max(case when rownum = 1 then item_label end) item_label1, 
    max(case when rownum = 1 then item_detail end) iitem_detail1, 
    max(case when rownum = 1 then item_score end) item_score1, 
    max(case when rownum = 2 then item_code end) item_code2, 
    max(case when rownum = 2 then item_label end) item_label2, 
    max(case when rownum = 2 then item_detail end) iitem_detail2, 
    max(case when rownum = 2 then item_score end) item_score2, 
    max(case when rownum = 3 then item_code end) item_code3, 
    max(case when rownum = 3 then item_label end) item_label3, 
    max(case when rownum = 3 then item_detail end) iitem_detail3, 
    max(case when rownum = 3 then item_score end) item_score3 
from 
(
    select group_id, item_code, item_detail, 
    item_label, item_score, 
    @rn:[email protected]+1 rownum 
    from yourtable, (SELECT @rn:=0) r 
    where group_id = 10 
    order by id 
) src 
group by group_id 

SQL Fiddle with Demo

2

你想要的東西,選擇所有行插入到Excel中一行。根據您的喜好在桌子上放置儘可能多的行。

SELECT 
    CONCAT(
     group_id,',', GROUP_CONCAT( 
      CONCAT_WS(',', item_code, item_label, item_detail, item_score) 
     ) 
    ) 
FROM thetable 

返回CSV:

10,BLU123,Blue 123,Blah blah 123,3,BLU124,Blue 124,Blah blah 124,6,BLU125,Blue 125,Blah blah 125,2 
+0

只是嘗試這個確切的解決方案(GROUP_CONCAT內嵌套的CONCAT除外)。如果我的數據不那麼長,這將會奏效。幾行之後,CSV就會被切斷。我會+1,但我太新了。謝謝回覆! – user1950195