【问题标题】:Select records only if all matching records are same value仅当所有匹配记录的值相同时才选择记录
【发布时间】:2021-08-07 12:34:36
【问题描述】:

我有下表:

id food type nutrition
1 chicken meat healthy
2 salad vegetable healthy
3 burger meat unhealthy

我想查询所有类型的完全健康的食物。这里的预期结果只是蔬菜,因为即使鸡肉是健康的,汉堡也被认为是不健康的。

【问题讨论】:

  • 你的 dbms 是什么? mysql还是postgresql?
  • 您应该指定预期结果,而不是描述它。
  • 还向我们展示您当前的查询尝试。
  • 如果预期结果只是蔬菜,那么为什么鸡肉会被标记为健康?
  • @d0little 鸡肉可能被认为是瘦白肉,而汉堡是红肉,脂肪含量更高。

标签: mysql sql postgresql


【解决方案1】:

在 MySQL 或 Postgres 中,我们可以尝试聚合:

SELECT type
FROM yourTable
GROUP BY type
HAVING SUM(nutrition <> 'healthy') = 0;

【讨论】:

    猜你喜欢
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多