首页 / MYSQL / 如何转置MySQL行和重复列标题?
如何转置MySQL行和重复列标题?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何转置MySQL行和重复列标题?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2913字,纯文字阅读大概需要5分钟。
内容图文
![如何转置MySQL行和重复列标题?](/upload/InfoBanner/zyjiaocheng/905/08766255c97941f291a0d2e446fbf09c.jpg)
我有一个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
结果:
| 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
内容总结
以上是互联网集市为您收集整理的如何转置MySQL行和重复列标题?全部内容,希望文章能够帮你解决如何转置MySQL行和重复列标题?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】