ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

如何转置MySQL行和重复列标题?

2019-06-29 21:03:02  阅读:214  来源: 互联网

标签:mysql transpose


我有一个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

谢谢大家!

解决方法:

如果这些是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:=@rn+1 rownum
  from yourtable, (SELECT @rn:=0) r
  where group_id = 10
  order by id
) src
group by group_id

SQL Fiddle with Demo

标签:mysql,transpose
来源: https://codeday.me/bug/20190629/1329629.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有