我得到了表orders
和order_comments
。每个订单可以有0到n条评论。我想以一个单独的顺序获得所有订单的列表及其评论。
表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行。评论应显示在单独的列中,从最早的评论开始。因此,在这种情况下所需的输出是:
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 - Rows to Columns - 但我不能使用'创建视图'。
我发现了这个:http://dev.mysql.com/doc/refman/5.5/en/while.html - 但我不能在这个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个订单。我也应该得到没有评论的订单。 我该怎么办?
答案 0 :(得分:3)
您可以为此类型的数据透视使用变量,但查询稍微复杂一些,因为您需要枚举每个订单的值:
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;