我必须从机场代码表中搜索并删除重复项。重复项不是完全重复的。一条记录可能是“标准”而另一条记录可能具有前导“K”。
代表同一机场的机场记录示例(为我们的目的重复):
column: identity
N57
KN57
我认为我可以通过从所有记录中删除前导“K”并使用此SQL将其与非剥离记录进行比较来实现此目的:
SELECT identity
FROM tbl_airports
WHERE identity IN (SELECT TRIM(LEADING 'K' FROM identity) FROM tbl_airports)
我的目标是返回没有前导“K”的版本,因此我可以将SQL转换为删除语句以删除重复的记录。
但是,上面的SQL似乎不起作用。有什么想法吗?
答案 0 :(得分:0)
如果要删除前导k
的记录,可以执行以下操作:
delete a
from tbl_airports a
where a.identity like 'K%' and
exists (select 1 from tbl_airports a2 where 'K' + a2.identity = a.identity);
这使用SQL Server语法,但类似的东西在大多数数据库中都可以使用。
使用" phpadmin"在标签中,这可能是MySQL。在该数据库中,您可以使用join
:
delete a join
(select 'K' + a2.identity as identity
from tbl_airports
where a2.identity not like 'K%'
) ak
on a.identity = ak.identity
where a.identity like 'K%';
编辑:
查询的select
版本为:
select a.*
from a join
(select 'K' + a2.identity as identity
from tbl_airports
where a2.identity not like 'K%'
) ak
on a.identity = ak.identity
where a.identity like 'K%';