简体   繁体   English

Select 仅当所有匹配记录的值相同时才记录

[英]Select records only if all matching records are same value

I have the following table:我有下表:

id ID food食物 type类型 nutrition营养
1 1 chicken meat healthy健康
2 2 salad沙拉 vegetable蔬菜 healthy健康
3 3 burger汉堡包 meat unhealthy不良

I want to query all types of food which are completely healthy.我想查询完全健康的所有类型的食物。 The expected result here is only vegetable because burger is considered unhealthy even if chicken is healthy.这里的预期结果只是蔬菜,因为即使鸡肉是健康的,汉堡也被认为是不健康的。

In MySQL or Postgres, we can try aggregating:在 MySQL 或 Postgres 中,我们可以尝试聚合:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM