gpt4 book ai didi

mysql - 从表中的同一列中选择不同的值并将其显示在不同的列下

转载 作者:行者123 更新时间:2023-11-29 10:15:38 25 4
gpt4 key购买 nike

我有一个名为“ORDER_HISTORY”的表,其中有一列名为“Status”。“Status”列包含不同商店的不同状态值。

我的要求是我需要获取每个商店的不同状态值(计数)并将其显示在相应的列中。例如:如果我有 53(order_cancelled)和 57(order_completed)作为两个状态,并且对于商店编号 20 我有两个状态为 57(即 order_completed)的订单和一个状态为 53(order_cancelled)的订单,然后我需要以如下格式显示结果(计数):

================================================
Store_ID || order_cancelled || order_completed||
------------------------------------------------
20 || 1 || 2 ||
================================================

如何编写执行上述工作的查询?

请帮忙。提前致谢!!

最佳答案

试试这个

SELECT a.Store_ID,b.total as order_completed,c.total as order_completed FROM ORDER_HISTORY a
LEFT OUTER join (select Store_ID,count(*) as total from ORDER_HISTORY where Status=57 group by Store_ID) b on a.Store_ID=b.Store_ID
LEFT OUTER join (select Store_ID,count(*) as total from ORDER_HISTORY where Status=53 group by Store_ID) c on a.Store_ID=c.Store_ID
group by a.Store_ID

关于mysql - 从表中的同一列中选择不同的值并将其显示在不同的列下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50148804/

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