MySQL CASE语句,语句列表中包含多个列

时间:2012-07-26 23:42:47

标签: mysql sql

我有这个查询,我知道它不起作用,但是我把它留下来,因为它是伪代码,以帮助解释我在做什么。我想在过去一周的星期几中从预订表中获得“预订”和“销售”总数。因此,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#'

如何在MySQL中完成这样的事情?

3 个答案:

答案 0 :(得分:3)

是的,但是案例只能返回一个值。你可以这样做:

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#'

你可能会发现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

答案 1 :(得分:1)

我想你想要这样的东西:

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

或者,如果您更喜欢等效(但更长)的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

CASE表达式的结果是标量;它不能返回多个值。

答案 2 :(得分:0)

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#'