我有一个有两列的表格CountryCode CountryName
。中存在重复条目countrycode
。但我想删除不重复的条目并保留重复的行countrycode
柱子。所以我想写一个SQL语句来做到这一点。我想我必须使用having,但不太确定如何准确地合并它。谢谢
这有点奇怪。我希望您想要删除重复的条目,而不是相反。但无论您使用什么数据库,这样的操作都应该有效:
delete from TableName
where CountryCode in (select CountryCode
from TableName
group by CountryCode
having count(*) = 1).
需要明确的是,子查询:
select CountryCode
from TableName
group by CountryCode
having count(*) = 1
...返回具有唯一性的行CountryCodes
。然后是delete
陈述:
delete from TableName
where CountryCode in (...)
...删除这些唯一的行,以便表中保留的唯一行应该是具有重复项的行。
但是,根据您的评论,听起来您只想要一个仅返回重复项的查询。如果是这种情况,那么只需在 a 中使用子查询select
声明,但修改having
仅返回重复项的子句:
select *
from TableName
where CountryCode in (select CountryCode
from TableName
group by CountryCode
having count(*) > 1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)