1

我有一个名为 food 的示例表

item    id    date
apple   1     in-date
apple   1     in-date
banana  2     rotten
banana  2     in-date
lemon   3     in-date
lemon   3     in-date

我想编写一个查询,它只返回所有内容都在日期的项目,即应该返回苹果和柠檬。

我有,

select item
from food
where date = 'in-date';

banana但是,即使有些已过时,这也会返回。任何帮助将非常感激

4

3 回答 3

7
select distinct item
from food
where item not in (select item from food where date<>'in-date')

或者,如果您的 SQL 支持exists

select distinct item
from food
where not exists (select item from food f where date<>'in-date' and f.item=food.item )
于 2012-11-19T14:17:21.313 回答
2
SELECT item, SUM( CASE mydate WHEN 'in_date' THEN 0 ELSE 1 END ) as rotten
FROM food
GROUP BY item 
  HAVING rotten==0
于 2012-11-19T14:21:45.907 回答
1

尝试这样的事情: -

 select distinct item
 from food
 where not exists (select item from food f where date<>'in-date' and f.item=food.item )
于 2012-11-19T14:18:06.153 回答