SQL:在MySQL中将行转换为列(SELECT语句) [英] SQL: transform rows into columns in MySQL (SELECT statement)

查看:389
本文介绍了SQL:在MySQL中将行转换为列(SELECT语句)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了表ordersorder_comments.每个订单可以包含0到n条注释.我想获取所有订单的清单,并按特定的顺序注明他们的意见.

I got table orders and order_comments. Each order can have from 0 to n comments. I would like to get list of all orders with their comments in a sepcific order.

orders:

order_id | order_nr 
1 | 5252
4 | 6783
5 | 6785


order_comments

id_order_comments | order_fk | created_at | email | content
1 | 4 | 2015-01-12 | jack | some text here
2 | 5 | 2015-01-13 | marta | some text here
3 | 5 | 2015-01-14 | beata | some text here
4 | 4 | 2015-01-16 | julia | some text here

因此,我希望每个订单获得1行.注释应从最早的注释开始在单独的列中显示.因此,在这种情况下,所需的输出是:

As a result, I would like to get 1 row for each order. Comments should be shown in separate columns, starting from the oldest comment. So desired output in this case is:

order_id | 1_comment_created_at | 1_comment_author | 1_comment_content | 2_comment_created_at | 2_comment_author | 2_comment_content 
1 | NULL | NULL | NULL | NULL | NULL | NULL
4 | 2015-01-12 | jack | some text here |  2015-01-16 | Julia | some text here
5 | 2015-01-13 | marta | some text here | 2015-01-14 | beata | some text here

我发现了这一点: MySQL-行到列-但是我不能使用'create view '.
我发现了这一点: http://dev.mysql.com/doc/refman/5.5/en/while.html -但我无法在此数据库中创建过程.

I found this: MySQL - Rows to Columns - but I cannot use 'create view'.
I found this: http://dev.mysql.com/doc/refman/5.5/en/while.html - but I cannot create procedure in this db.


我所得到的:

SELECT @c := (SELECT count(*) FROM order_comments GROUP BY order_fk ORDER BY count(*) DESC LIMIT 1);

SET @rank=0;
SET @test=0;

SELECT
  CASE WHEN @test < @c AND temp.comment_id = @test THEN temp.created_at END AS created,
  CASE WHEN @test < @c AND temp.comment_id = @test THEN temp.author END AS author,
  CASE WHEN @test < @c AND temp.comment_id = @test THEN temp.content END AS content
  /*But I cannot set @test as +1. And I cannot name column with variable - like CONCAT(@test, '_created')*/

FROM (
  SELECT @rank := @rank +1 AS comment_id, created_at, author, content
  FROM order_comments 
  WHERE order_fk = 4
  ORDER BY created_at
) AS temp

问题:我想搜索1个以上的订单.我也应该收到没有评论的订单. 我该怎么办?

Problem: I would like to search more than 1 order. I should get orders with no comments too. What can I do?

推荐答案

您可以将变量用于这种类型的数据透视,但是查询要复杂一些,因为您需要枚举每个订单的值:

You can use variables for this type of pivot, but the query is a bit more complicated, because you need to enumerate the values for each order:

SELECT o.order_id,
       MAX(case when rank = 1 then created_at end) as created_at_1,
       MAX(case when rank = 1 then email end) as email_1,
       MAX(case when rank = 1 then content end) as content_1,
       MAX(case when rank = 2 then created_at end) as created_at_2,
       MAX(case when rank = 2 then email end) as email_2,
       MAX(case when rank = 2 then content end) as content_2,
FROM orders o LEFT JOIN
     (SELECT oc.*,
             (@rn := if(@o = order_fk, @rn + 1,
                        if(@o := order_fk, 1, 1)
                       )
             ) as rank
      FROM order_comments oc CROSS JOIN
           (SELECT @rn := 0, @o := 0) vars
      ORDER BY order_fk, created_at
     ) oc
     ON o.order_id = oc.order_fk
GROUP BY o.order_id;

这篇关于SQL:在MySQL中将行转换为列(SELECT语句)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆