我具有以下格式的MySQL表
id c1 c2
---------------------
id1 c1v1 c2v1 -> keep id1 even it has duplicates
id1 c1v1 c2v1
id2 c1v2 c2v2 -> filter out id2 because it has various c1, c2
id2 c1v2 c2v3
id3 c1v3 c2v4
....
预期输出:
id c1 c2
---------------------
id1 c1v1 c2v1
id3 c1v3 c2v4
....
我只想保留id仅具有唯一的c1,c2值的记录。 我确实有解决方案,但是它需要两次全表扫描,包括一次联接,效率非常低,我想知道是否有更好的方法可以做到这一点。
select
distinct id, c1, c2
from table
inner join
(select
id,
count(distinct c1, c2) as counts
from table
group by id
having counts = 1) tmp
on table.id = tmp.id
答案 0 :(得分:1)
仅使用group by
:
select id, min(c1) as c1, min(c2) as c2
from t
group by id
having min(c1) = max(c1) and min(c2) = max(c2)
如果要获取所有原始记录,请使用not exists
select t.*
from t
where not exists (select 1
from t t2
where t2.id = t.id and
(t2.c1 <> t.c1 or t2.c2 <> t.c2)
);
答案 1 :(得分:1)
您可以在不同的c1 / c2值上使用自我LEFT JOIN
来执行此操作,仅在第二张表中保留没有匹配行的行(即,同一id
的值不同):
SELECT DISTINCT t1.id, t1.c1, t1.c2
FROM test t1
LEFT JOIN test t2 ON t2.id = t1.id AND (t2.c1 != t1.c1 OR t2.c2 != t1.c2)
WHERE t2.id IS NULL
输出:
id c1 c2
id1 c1v1 c2v1
id3 c1v3 c2v4