【问题标题】:Postgresql selecting all rows which don't have any of a set of valuesPostgresql 选择没有任何一组值的所有行
【发布时间】:2020-06-22 09:20:10
【问题描述】:

我有一张名为 Meals 的桌子。膳食有许多成分(例如土豆泥、西兰花、牛排),每个成分都有_many过敏原。

我想选择没有任何给定过敏原列表的膳食。

如果我加入这三个表,并且 allergen_id not in (... list of allergen ids),如果它有任何不在要排除的过敏原列表中的过敏原,那将返回给我一顿饭,即不对:

select * from meals m 
join components c on c.meal_id=c.id 
join allergens a on a.component_id=c.id
where a.id not in (1,2,3,4)

如果一餐有过敏原 1 5 和 7,仍然会从上面的查询中返回。怎么说“退还我所有不含以下任何过敏原的餐食”

更新说明性示例。

假设我吃了一顿由土豆泥、豌豆和馅饼组成的餐点。马什里面有奶制品,馅饼里面有麸质。然后我吃了一顿含有肉类过敏原的牛排餐(对素食主义者没有好处)。

土豆泥、豌豆、馅饼 -> 乳制品、麸质

牛排、薯条 -> 肉

我想选择不含奶制品的餐点。结果应该只是牛排/薯条餐。

如果我加入他们:

select * from meals m 
join components c on c.meal_id=c.id 
join allergens a on a.component_id=c.id

这将为我列出每种过敏原。所以如果我添加 where 子句:

select * from meals m 
join components c on c.meal_id=c.id 
join allergens a on a.component_id=c.id
where a.id not in (2)

面筋将排成一排,因此不排除土豆泥/豌豆/馅饼餐。如何排除?

【问题讨论】:

  • 请尝试通过详细说明输入数据和预期输出结果来阐明您的示例(如果可能的话,可能使用更少的表格)。
  • 好的,我添加了更多内容来尝试澄清,这有帮助吗?

标签: sql postgresql postgresql-10


【解决方案1】:

NOT EXISTS(...) 完全符合您的要求。

你自己的文字I want to select the meal(s) which doesn't have any of a given list of allergens.几乎可以直接翻译成SQL:


 -- How do I say:
 -- Return me all meals
select * from meals m
where not exists ( --  which doesn't have 
        select * 
        from  components c  -- any of the following allergens
        join allergens a on a.component_id = c.id AND a.id in (1,2,3,4)
        where c.meal_id = m.id
        );

【讨论】:

  • 哦该死的TIL。太棒了,我正在走 CTE 路线,这让我毛骨悚然,谢谢。
猜你喜欢
  • 2018-10-06
  • 2021-11-21
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
相关资源
最近更新 更多