在 mysql 查询中枢轴

pivot in mysql queries(在 mysql 查询中枢轴)
本文介绍了在 mysql 查询中枢轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有下面给出的查询,输出如下:

We have the query which is given below and the output is as follows:

mysql> select count(1) as cnt ,card.Program_ID program_id ,fd.load_type load_type ,
    -> if(fd.load_type =0 ,concat('unload_cl_proram_',card.program_id) ,concat('load_cl_proram_',card.program_id)) as load_desc
    -> from transaction_log_details fd inner join card card
    -> on fd.Card_ID = card.Card_ID where card.Date_Created>='2011-01-01' and card.Date_Created<='2011-06-01'
    -> and card.Program_ID in (4,5,6,7,8)
    -> group by card.Program_ID,fd.load_type;
+-----+------------+-----------+--------------------+
| cnt | program_id | load_type | load_desc          |
+-----+------------+-----------+--------------------+
|   5 |          4 | 0         | unload_cl_proram_4 |
|  14 |          4 | 1         | load_cl_proram_4   |
|  55 |          5 | 0         | unload_cl_proram_5 |
|  91 |          5 | 1         | load_cl_proram_5   |
|  13 |          6 | 0         | unload_cl_proram_6 |
|  29 |          6 | 1         | load_cl_proram_6   |
| 175 |          7 | 0         | unload_cl_proram_7 |
| 411 |          7 | 1         | load_cl_proram_7   |
|  61 |          8 | 0         | unload_cl_proram_8 |
| 161 |          8 | 1         | load_cl_proram_8   |
+-----+------------+-----------+--------------------+
10 rows in set (0.00 sec)


Now we want the output as follows: (some thing like pivot).
unload_cl_proram_4  load_cl_proram_4    unload_cl_proram_5  load_cl_proram_5    unload_cl_proram_6  load_cl_proram_6    unload_cl_proram_7  load_cl_proram_7    unload_cl_proram_8  load_cl_proram_8    
5               14          55          91          13          29        175   411 61  161 

推荐答案

使用

案例

声明

参考资料 -

类似问题 - SQL 查询使用 CASE 旋转列当

http://www.sqlshare.com/pivot-using-the-case-statement_524.aspx(视频)

这篇关于在 mysql 查询中枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)