我有一张这样的桌子......
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上的表格中写下我的问题所以请忽略我错误的询问方式。
感谢
答案 0 :(得分:5)
尝试以下:
SELECT * FROM `table`
WHERE id = 1 AND oid NOT IN (SELECT oid FROM `table` where id != 1)
答案 1 :(得分: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 ║
╚═════╝
答案 2 :(得分:2)
select count(oid), oid from table
where id = 1
group by oid
having count(oid) = 1
答案 3 :(得分:1)
SELECT *
FROM TABLE_NAME
WHERE id = 1
AND oid NOT IN (SELECT DISTINCT oid FROM TABLE_NAME where id != 1)