Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql查询中的pivot_Mysql_Sql_Pivot - Fatal编程技术网

mysql查询中的pivot

mysql查询中的pivot,mysql,sql,pivot,Mysql,Sql,Pivot,我们有下面给出的查询,输出如下: 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

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

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 
mysql>选择count(1)作为cnt,card.Program\u ID Program\u ID,fd.load\u type load\u type,
->如果(fd.load_type=0,concat('unload_cl_proram_',card.program_id),concat('load_cl_proram_',card.program_id))作为加载描述
->从交易记录详细信息fd内部加入卡
->在fd.Card_ID=Card.Card_ID,其中Card.Date_Created>='2011-01-01'和Card.Date_Created和Card.Program_ID in(4,5,6,7,8)
->按卡分组。程序ID,fd。加载类型;
+-----+------------+-----------+--------------------+
|cnt |程序| id |加载类型|加载说明|
+-----+------------+-----------+--------------------+
|5 | 4 | 0 |卸载程序4|
|14 | 4 | 1 |装载程序4|
|55 | 5 | 0 |卸载程序5|
|91 | 5 | 1 |装载程序5|
|13 | 6 | 0 |卸载程序6|
|29 | 6 | 1 |装载程序6|
|175 | 7 | 0 |卸载程序| 7|
|411 | 7 | 1 |装载程序7|
|61 | 8 | 0 |卸载程序8|
|161 | 8 | 1 |装载程序8|
+-----+------------+-----------+--------------------+
一组10行(0.00秒)
现在我们希望输出如下:(一些像pivot的东西)。
卸载项目4装载项目4卸载项目5装载项目5卸载项目6装载项目6卸载项目7卸载项目8装载项目8装载项目8
5               14          55          91          13          29        175   411 61  161 
使用

声明

参考资料-

类似问题-

(视频)

使用

声明

参考资料-

类似问题-


(视频)

能否在表格数据周围添加代码标记?请注意,您可以将它们缩进四个空格。编辑器工具栏中的“{}”按钮可以为您执行此操作。编辑您的问题并进行尝试。单击编辑器工具栏中的橙色问号,了解有关格式设置的详细信息和提示。对于输出,使用
元素,直到SO支持为止。另外,给自己一个。它显示您已提交,并允许其他人使用“@”向您发送地址。请注意,SQL语句应该是代码,这意味着省略MySQL提示。编辑您的问题并尝试。您可以在表格数据周围添加代码标记吗?请注意,您可以将它们缩进四个空格。编辑器工具栏中的“{}”按钮可以为您执行此操作。编辑您的问题并进行尝试。单击编辑器工具栏中的橙色问号,了解有关格式设置的详细信息和提示。对于输出,使用
元素,直到SO支持为止。另外,给自己一个。它显示您已提交,并允许其他人使用“@”向您发送地址。请注意,SQL语句应该是代码,这意味着省略MySQL提示。编辑您的问题并尝试。