根据两列选择项目

问题描述

我有一个数据库...

descriptionID   | ItemID  |  description 
--------------------------------------------  
1               |   1     |    Blue
2               |   1     |    Small
3               |   1     |    Circular
4               |   2     |    Blue
5               |   2     |    Large
6               |   3     |    Small
7               |   4     |    Blue
...                 ...           ...

我想返回itemID,其中有蓝色且很小的条目,因此返回itemID = 1,没有其他itemID。

ItemID
--------------
   1

解决方法

您可以使用聚合。假设没有重复:

select itemid
from t
where description in ('Blue','Small')
group by itemid
having count(*) = 2;
,

按照您的要求写出来。

描述为Blue的项目和(设置相交)描述为Small的项目

select itemId from tab where description = 'Blue'
INTERSECT
select itemId from tab where description = 'Small';
,

您可以使用以下内容:

SELECT DISTINCT itemId FROM tbl t1
WHERE EXISTS (SELECT * FROM tbl WHERE description = 'Blue' AND itemId = t1.itemId)
    AND EXISTS (SELECT * FROM tbl WHERE description = 'Small' AND itemId = t1.ItemId)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...