[英]SQL: transform rows into columns in MySQL (SELECT statement)
I got table orders
and order_comments
. 我得到表orders
和order_comments
。 Each order can have from 0 to n comments. 每个订单可以包含0到n条注释。 I would like to get list of all orders with their comments in a sepcific order. 我想获得所有订单的清单,并以特定顺序列出他们的评论。
Table orders
: 表orders
:
order_id | order_nr
1 | 5252
4 | 6783
5 | 6785
Table order_comments
表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
As a result, I would like to get 1 row for each order. 结果,我想为每个订单获得1行。 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
I found this: MySQL - Rows to Columns - but I cannot use 'create view'. 我发现了这一点: MySQL-行到列 -但我不能使用“创建视图”。
I found this: http://dev.mysql.com/doc/refman/5.5/en/while.html - but I cannot create procedure in this db. 我发现了这一点: http : //dev.mysql.com/doc/refman/5.5/en/while.html-但我无法在此数据库中创建过程。
What I got : 我得到了 :
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
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.