我有下表 - mytable
:
----id----|----user_id----|----value-----|
----1-------------1------------- 9
----2-------------1------------- 8
----3-------------2 -------------11
----4-------------2-------------12
----5-------------3-------------11
----6-------------4-------------8
----7-------------1-------------3
我想SELECT * FROM mytable...
其中value有一条记录如下:
----id----|----user_id----|----value-----
----1-------------1-------------9
----4-------------2-------------12
----7-------------1-------------3
请注意,值8和值11有重复项,我删除了所有
答案 0 :(得分:1)
试试这个
SELECT * FROM mytable
GROUP BY `value`
HAVING count(*) = 1
或
SELECT * FROM mytable
GROUP BY `value`
HAVING count(*) < 2
答案 1 :(得分:1)
您可以尝试左连接:
SELECT T1.id, T1.user_id, T1.value
FROM yourtable T1
LEFT JOIN yourtable T2
ON T1.value = T2.value
AND T1.id <> T2.id
WHERE T2.id IS NULL
答案 2 :(得分:0)
以下查询应该适用于上面提到的问题..
SELECT T1。* 从MYTABLE T1, ( 选择价值 来自MYTABLE 按价值分组 有COUNT(*)= 1 )T2 在哪里T1.VALUE = T2.VALUE;
此致 Venk
答案 3 :(得分:0)
我会这样做:
select t1.*
from table1 t1
where 1 = (select count(t2.id)
from table1 t2
where t1.value = t2.value
)
希望它有效。
PS:id必须是唯一的密钥。