简体   繁体   English

MySQL CASE语句,在语句列表中有多列

[英]MySQL CASE Statement with multiple columns in statement list

I have this query, which I know doesn't work, but I've left it as it is as pseudo-code to help explain what I'm doing. 我有这个查询,我知道这行不通,但是我把它保留为伪代码,以帮助解释我在做什么。 I'm trying to get "Booking" and "Sales" totals from a Booking table by day-of-the-week for the past week. 我试图在过去一周的一周中的某天从“预订”表中获取“预订”和“销售”总计。 Hence, Mon1B = Bookings for Monday and Mon1S = Sales for Monday. 因此,Mon1B =星期一的预订,Mon1S =星期一的销售。

SELECT  
   CASE WEEKDAY(b.created)
   WHEN 0 THEN (SELECT COUNT(uuid) as Mon1B, SUM(amount) as Mon1S)
   WHEN 1 THEN (SELECT COUNT(uuid) as Tue1B, SUM(amount) as Tue1S)
   WHEN 2 THEN (SELECT COUNT(uuid) as Wed1B, SUM(amount) as Wed1S)
   WHEN 3 THEN (SELECT COUNT(uuid) as Thu1B, SUM(amount) as Thu1S)
   WHEN 4 THEN (SELECT COUNT(uuid) as Wed1B, SUM(amount) as Wed1S)
   WHEN 5 THEN (SELECT COUNT(uuid) as Wed1B, SUM(amount) as Wed1S)
   WHEN 6 THEN (SELECT COUNT(uuid) as Wed1B, SUM(amount) as Wed1S)
END CASE
FROM Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'

How can something like this be done in MySQL? 在MySQL中如何做这样的事情?

Yes, but case can only return one value. 是的,但是大小写只能返回一个值。 You can do it like this: 您可以这样做:

SELECT sum(CASE when WEEKDAY(b.created) = 0 then 1 else 0 end) as Mon1b,
       sum(case when weekday(b.created) = 0 then amount else 0 end) as Mon1S,
       ...
FROM Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'

You might find it easier as 7 rows, though: 不过,您可能会发现7行更容易:

select WEEKDAY(b.created), count(*) as cnt, sum(amount) as amt
from Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'
group by WEEKDAY(b.created)
order by 1

I think you want something like this: 我想你想要这样的东西:

SELECT COUNT(IF(WEEKDAY(b.created)=0,uuid,NULL)) AS Mon1B
     , SUM(IF(WEEKDAY(b.created)=0,amount,NULL)) AS Mon1S
     , COUNT(IF(WEEKDAY(b.created)=1,uuid,NULL)) AS Tue1B
     , SUM(IF(WEEKDAY(b.created)=1,amount,NULL)) AS Tue1S

Or, if you prefer the equivalent (but lengthier) CASE expression: 或者,如果您希望使用等价(但更长)的CASE表达式:

SELECT COUNT(CASE WEEKDAY(b.created) WHEN 0 THEN uuid END) AS Mon1B
     , SUM(CASE WEEKDAY(b.created) WHEN 0 THEN amount END) AS Mon1S
     , COUNT(CASE WEEKDAY(b.created) WHEN 1 THEN uuid END) AS Tue1B
     , SUM(CASE WEEKDAY(b.created) WHEN 1 THEN amount END) AS Tue1S

The result of a CASE expression is a scalar; CASE表达式的结果是一个标量。 it can't return more than one value. 它不能返回多个值。

SELECT  WEEKDAY(b.created),
   CASE 
   WHEN b.weekday='Monday' THEN (SELECT COUNT(b.uuid) as Mon1B, SUM(s.amount) as Mon1S from bookings b,sales s      where b.day='Monday' and s.day='Monday')
   WHEN b.weekday='Tuesday' THEN (SELECT COUNT(b.uuid) as Tue1B, SUM(s.amount) as Tue1 from bookings b,sales s      where b.day='Tuesday' and s.day='tuesday')
   WHEN b.weekday='Wednesday' THEN (SELECT COUNT(b.uuid) as Wed1B, SUM(s.amount) as Wed1S from bookings b,sales s      where b.day='wednesday' and s.day='wednesday')
   WHEN b.weekday='Thursday' THEN (SELECT COUNT(b.uuid) as Thu1B, SUM(s.amount) as Thu1S from bookings b,sales s      where b.day='Thursday' and s.day='Thursday')
   WHEN b.weekday='Friday' THEN (SELECT COUNT(b.uuid) as Fri1B, SUM(s.amount) as Fri1S from bookings b,sales s      where b.day='Friday' and s.day='Friday')
   WHEN b.weekday='saturaday' THEN (SELECT COUNT(b.uuid) as Sat1B, SUM(s.amount) as sat1S from bookings b,sales s      where b.day='Saturaday' and s.day='Saturaday')
   WHEN b.weekday='Sunday' THEN (SELECT COUNT(b.uuid) as sun1B, SUM(s.amount) as sun1S from bookings b,sales s      where b.day='Sunday' and s.day='Sunday')
END CASE
FROM Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM