MariaDB Find Where Count()= 0

时间:2018-05-18 02:21:26

标签: count mariadb is-empty

我试图找到哪些日子没有主菜(recipe_course = 18),但我得到的只是零结果。我错过了什么?

Two tables
table recipes{recipe_id, recipe_name, recipe_course}
table meals{meal_id, meal_date, meal_recipe}

Select `meal_date`
From `meals` mls
Join `recipes` rrr on mls.`meal_recipe` = rrr.`recipe_id`
Where rrr.`recipe_course` = 18
Group by `meal_date`
Having Count(`recipe_course`) = 0

2 个答案:

答案 0 :(得分:0)

最简单的方法是使用"而不是"。 " =查找没有recipe_course 18"

的所有用餐日期
select m.meal_date
from meals m
where m.meal_recipe not in (
  select r.recipe_id
  from recipes r
  where r.recipe_course = 18
)

答案 1 :(得分:0)

或者,使用LEFT JOIN ... IS NULL

SELECT  m.meal_date
    FROM  meals m
    LEFT JOIN  recipes r  ON r.recipe_id = m.meal_recipe
      AND  r.recipe_course = 18
    WHERE  r.recipe_id IS NULL

也可以使用NOT EXISTS ( SELECT * ... )