4

我有一张这样的桌子...

oid      id 
35       1  
43       1  
46       1 
43       2  
49       2

我有id=1现在我想要那些只属于 1 而不是任何其他 id 的记录。

即 o/p - 35,46

我不需要 oid = 43 bcz 它也属于 2。

我不知道如何在stackoverflow上的表格中写下我的问题,所以请忽略我错误的提问方式。

谢谢

4

4 回答 4

5

试试下面:

SELECT * FROM `table`
WHERE id = 1 AND oid NOT IN (SELECT oid FROM `table` where id != 1)
于 2013-06-18T09:21:36.050 回答
3

这是另一种方式,

SELECT  oid
FROM    tableName
GROUP   BY oid
HAVING  COUNT(DISTINCT id) = 1 AND  -- counts the number of ID for OID
        MAX(ID) = 1                 -- checks if the value of ID is equal to 1

输出

╔═════╗
║ OID ║
╠═════╣
║  35 ║
║  46 ║
╚═════╝
于 2013-06-18T09:25:23.940 回答
2
select count(oid), oid from table
where id = 1
group by oid
having count(oid) = 1
于 2013-06-18T09:18:34.267 回答
1
SELECT * 
FROM TABLE_NAME
WHERE id = 1 
AND oid NOT IN (SELECT DISTINCT oid FROM TABLE_NAME where id != 1)
于 2013-06-18T09:29:01.967 回答