我有一个样本表”
A | 2
A | 0
A | 0
B | 1
B | 1
C | 2
C | 1
我希望结果是:
A | 2
A | 0
A | 0
C | 2
C | 1
即,我不希望第二列中具有相同值的行
假设您的表有字符列“ch”和数字列“值”,您可以执行以下操作:
select t.ch, t.value
from test t
join (
select ch, count(distinct value) as 'dst'
from test
group by ch
having count(distinct value)=1
) as q on t.ch!=q.ch;
子查询找到只有一个不同值的 ch。这些不包括在联接中。
不是最有效的查询,但我想你想要这样的东西:
SELECT * FROM test WHERE C1 NOT IN (
SELECT C1 FROM test
GROUP BY C1 HAVING COUNT(DISTINCT(C2)) = 1
);
我假设您不希望仅包含 1 列示例的记录;好吧,如果你这样做:
(例如,如果有一个“D | 1”行并且你想要那个)
SELECT * FROM test WHERE C1 NOT IN (
SELECT C1 FROM test
GROUP BY C1 HAVING COUNT(DISTINCT(C2)) = 1 AND COUNT(C2) > 1
);
编辑:这是sqlfiddle 链接