【发布时间】:2015-07-20 17:04:40
【问题描述】:
我的表只包含 2 列 ID 和 product,如下所示:
ID Product
-----------------
1 microsoft
0 cisco
2 cisco
7 cisco
3 vmware
0 adobe
0 microsoft
我需要编写一个仅列出 id=0 记录的查询,
ID Product
---------------
0 adobe
不想列出的原因
ID Product
----------------
0 micrsoft
0 cisco
是因为他们有一个或多个具有产品 ID 的记录。
试过这个:
SELECT
[ProductID], [Product]
FROM [table] AS t1
WHERE ProductID = 0
AND NOT EXISTS (SELECT 1
FROM [table] AS t2
WHERE t1.ProductID = t2.ProductID
AND t2.ProductID <> 0)
似乎需要很长时间才能查询。 (表有 2 000 000 条记录)
【问题讨论】:
-
你为什么又问同样的问题? stackoverflow.com/questions/31484775/…
标签: sql sql-server tsql unique where-clause