假设我的表只包含两个字段,ID和product,并且假设我的结构如下所示
1._
0中转机
2. cisco
3.中转站
0.不合格土地
0.=‘3’>产品
我需要编写一个查询,其中列出了具有id=0的记录,但是,
0
因为另外两种产品--迈格拉软和思科--都有另一种有ID的记录,所以我可以排除它们。
我希望我对我想要达到的目标是清楚的。请让我知道这是否可以在sql中完成。
发布于 2015-07-17 21:14:10
是的,使用基本聚合是可能的。注意到我是如何发布可消费数据的吗?这是你应该做的。创建可用数据比编写查询花费的时间要长得多。
with something(Col1, Col2) as
(
select 1, 'microsoft' union all
select 0, 'cisco' union all
select 2, 'cisco' union all
select 3, 'vmware' union all
select 0, 'adobe' union all
select 0, 'microsoft'
)
select Col2
from something
group by Col2
having MAX(Col1) = 0发布于 2015-07-17 21:15:25
您可以使用NOT EXISTS
SELECT id, product
FROM mytable AS t1
WHERE id = 0 AND NOT EXISTS (SELECT 1
FROM mytable AS t2
WHERE t1.product = t2.product AND t2.id <> 0)Demo here
发布于 2015-07-17 21:14:37
试试像这样的东西
SELECT *
FROM table
WHERE id = 0 and product not in
(
SELECT product
FROM table
WHERE id <> 0
)https://stackoverflow.com/questions/31484775
复制相似问题