简体   繁体   中英

Select records only if all matching records are same value

I have the following table:

id food type nutrition
1 chicken meat healthy
2 salad vegetable healthy
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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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