使用COUNT

时间:2016-10-24 20:52:39

标签: count

列出预订时出现的日期,只有红色 已使用COUNT聚合函数保留了船只。 这是表

仆人表:

sname rating
Joe 4
Bob 2
Tim 9
Mike 1
Lewis 5

船桌:

bname color rating
Ace orange 6
Bethany red 5
Cruiser green 9
WindySea red 8

预订表:

sname bname day
Bob  Ace Monday
Bob  Bethany Wednesday
Bob  WindySea Saturday
Tim  Ace Sunday
Tim  Bethany Wednesday
Tim  Cruiser Wednesday
Mike Ace Monday

1 个答案:

答案 0 :(得分:1)

SELECT r.day 
FROM reservation r
JOIN boat b
  ON r.bname = b.bname
GROUP BY r.day 
HAVING COUNT(CASE WHEN b.color <> 'red' THEN 1 ELSE NULL END) = 0
   AND COUNT(CASE WHEN b.color = 'red' THEN 1 ELSE NULL END) > 0 -- optional