如何实现mysql行转列
下面一起来了解下如何实现mysql行转列,相信大家看完肯定会受益匪浅,文字在精不在多,希望如何实现mysql行转列这篇短内容是你想要的。
原始数据如下:
mysql> select id,sdkname,sid,date,total_count from u1ge_query_log;
+------+----------------+------+------------+-------------+
| id | sdkname | sid | date | total_count |
+------+----------------+------+------------+-------------+
| 521 | ICC_ICCGAME(3) | 11 | 2017-05-01 | 0 |
| 522 | ICC_ICCGAME(3) | 11 | 2017-05-02 | 0 |
| 523 | ICC_ICCGAME(3) | 11 | 2017-05-03 | 1 |
| 531 | ICC_HUAWEI | 11 | 2017-05-01 | 0 |
| 532 | ICC_HUAWEI | 11 | 2017-05-02 | 0 |
| 533 | ICC_HUAWEI | 11 | 2017-05-03 | 0 |
| 541 | ICC_ICCGAME(0) | 11 | 2017-05-01 | 0 |
| 542 | ICC_ICCGAME(0) | 11 | 2017-05-02 | 0 |
| 543 | ICC_ICCGAME(0) | 11 | 2017-05-03 | 0 |
| 551 | ICC_UC | 11 | 2017-05-01 | 0 |
| 552 | ICC_UC | 11 | 2017-05-02 | 0 |
| 553 | ICC_UC | 11 | 2017-05-03 | 0 |
| 561 | ICC_QIHOO | 11 | 2017-05-01 | 0 |
| 562 | ICC_QIHOO | 11 | 2017-05-02 | 0 |
| 563 | ICC_QIHOO | 11 | 2017-05-03 | 0 |
| 571 | ICC_VIVO | 11 | 2017-05-01 | 0 |
| 572 | ICC_VIVO | 11 | 2017-05-02 | 0 |
| 573 | ICC_VIVO | 11 | 2017-05-03 | 0 |
| 581 | ICC_GIONEE | 11 | 2017-05-01 | 0 |
| 582 | ICC_GIONEE | 11 | 2017-05-02 | 0 |
| 583 | ICC_GIONEE | 11 | 2017-05-03 | 0 |
要求如下显示:
此处用到了行转列,由于列不固定,考虑使用动态sql
SET @d=(select GROUP_CONCAT('if(sdkname=\'',sdkname,'\' ,total_count,0) as `',sdkname,'`')
FROM (SELECT DISTINCT sdkname FROM pcik_log_dep.u1ge_query_log ) A);
SET @sql=CONCAT('select date,',@d,'from pcik_log_dep.u1ge_query_log group by date');
PREPARE sdtmt from @sql;
EXECUTE sdtmt;
deallocate prepare sdtmt;
------------+---------+-----------+-------------+------------+------------+----------------+----------------+------------+-----------+----------+-----------+--------+----------+------------+
| date | icctest | ICC_BAIDU | ICC_COOLPAD | ICC_GIONEE | ICC_HUAWEI | ICC_ICCGAME(0) | ICC_ICCGAME(3) | ICC_LENOVO | ICC_MEIZU | ICC_OPPO | ICC_QIHOO | ICC_UC | ICC_VIVO | ICC_XIAOMI |
+------------+---------+-----------+-------------+------------+------------+----------------+----------------+------------+-----------+----------+-----------+--------+----------+------------+
| 2017-05-01 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-02 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-03 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-04 | 0 | 0 | 0 | 0 | 0 | 0 | 4380 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-05 | 0 | 0 | 0 | 0 | 0 | 0 | 5126 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-06 | 0 | 0 | 0 | 0 | 0 | 0 | 5571 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-07 | 0 | 0 | 0 | 0 | 0 | 0 | 5888 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-08 | 0 | 0 | 0 | 0 | 0 | 0 | 6135 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-09 | 0 | 0 | 0 | 0 | 0 | 0 | 6199 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017-05-10 | 0 | 0 | 0 | 0 | 0 | 0 | 6199 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+------------+---------+-----------+-------------+------------+------------+----------------+----------------+------------+-----------+----------+-----------+--------+----------+------------+
10 rows in set (0.00 sec)
看完如何实现mysql行转列这篇文章后,很多读者朋友肯定会想要了解更多的相关内容,如需获取更多的行业信息,可以关注我们的数据库栏目。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341