gpt4 book ai didi

mysql - SQL : transform rows into columns in MySQL (SELECT statement)

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:01 24 4
gpt4 key购买 nike

我得到了表 ordersorder_comments。每个订单可以有 0 到 n 条评论。我想按特定顺序获取所有订单及其评论的列表。

订单:

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 - 但我不能使用“创建 View ”。
我发现了这个:http://dev.mysql.com/doc/refman/5.5/en/while.html - 但我无法在此数据库中创建过程。


我得到了什么:

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 个订单。我也应该得到没有评论的订单。我能做什么?

最佳答案

您可以为这种类型的数据透视表使用变量,但查询有点复杂,因为您需要枚举每个订单的值:

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;

关于mysql - SQL : transform rows into columns in MySQL (SELECT statement),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28066983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com