Return 布尔值(如果存在重复项)
Return boolean if duplicates exist
如何 return True 如果 table 有任何重复记录,False 如果它有none。
I don't need a count, I don't need to know which rows, all I want to know is if this table has duplicates or not. That's it.
如果您不想列出列,那么这是一种方法:
select (count(*) <> num_distinct) as has_duplicates
from t cross join
(select count(*) as num_distinct
from (select distinct * from t) t
) tt;
如果您有主键,则更有效的方法是:
select (count(*) <> count(distinct pk)) as has_duplicates
from t;
一个相对有效的主键方法是:
select (count(*) = 1) as has_duplicates
from (select t.*
from t
where exists (select 1 from t t2 where t2.pk = t.pk and t2.? <> t.?)
fetch first 1 row only
) t;
?
是您关心的 column/columns 重复项。
你可以试试
SELECT COUNT(*) > 1
FROM T
GROUP BY C1,C2...
如何 return True 如果 table 有任何重复记录,False 如果它有none。
I don't need a count, I don't need to know which rows, all I want to know is if this table has duplicates or not. That's it.
如果您不想列出列,那么这是一种方法:
select (count(*) <> num_distinct) as has_duplicates
from t cross join
(select count(*) as num_distinct
from (select distinct * from t) t
) tt;
如果您有主键,则更有效的方法是:
select (count(*) <> count(distinct pk)) as has_duplicates
from t;
一个相对有效的主键方法是:
select (count(*) = 1) as has_duplicates
from (select t.*
from t
where exists (select 1 from t t2 where t2.pk = t.pk and t2.? <> t.?)
fetch first 1 row only
) t;
?
是您关心的 column/columns 重复项。
你可以试试
SELECT COUNT(*) > 1
FROM T
GROUP BY C1,C2...