我有一个示例表“
A | 2
A | 0
A | 0
B | 1
B | 1
C | 2
C | 1
我希望结果是:
A | 2
A | 0
A | 0
C | 2
C | 1
即, 我不希望第二列中具有相同值的行
答案 0 :(得分: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。这些不包括在加入中。
答案 1 :(得分:0)
不是最有效的查询,但我想你想要这样的东西:
SELECT * FROM test WHERE C1 NOT IN (
SELECT C1 FROM test
GROUP BY C1 HAVING COUNT(DISTINCT(C2)) = 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 link