我已经给客户端以下查询删除重复的电话号码.MSSQL数据库中的记录,但现在他们还需要在MySQL上执行此操作,并且他们报告MySQL抱怨查询的格式.我已经为我的代码示例包含了一个带有重复项的测试表的设置,但实际的删除查询才是最重要的.
我在无知和紧迫的情况下问这个,因为我还在忙着下载和安装MySQL,也许有人可以帮忙.
create table bkPhone ( phoneNo nvarchar(20), firstName nvarchar(20), lastName nvarchar(20) ) GO insert bkPhone values('0783313780','Brady','Kelly') insert bkPhone values('0845319792','Mark','Smith') insert bkPhone values('0834976958','Bill','Jones') insert bkPhone values('0845319792','Mark','Smith') insert bkPhone values('0828329792','Mickey','Mouse') insert bkPhone values('0834976958','Bill','Jones') alter table bkPhone add phoneId int identity delete from bkPhone where phoneId not in ( select min(phoneId) from bkPhone group by phoneNo,firstName,lastName having count(*) >= 1 )
Tom Schaefer.. 14
许多方式导致罗马.这是一.它非常快.所以你可以在大数据库中使用它.别忘了它们.诀窍是:make phoneNo unique并使用"ignore".
drop table if exists bkPhone_template; create table bkPhone_template ( phoneNo varchar(20), firstName varchar(20), lastName varchar(20) ); insert into bkPhone_template values('0783313780','Brady','Kelly'); insert into bkPhone_template values('0845319792','Mark','Smith'); insert into bkPhone_template values('0834976958','Bill','Jones'); insert into bkPhone_template values('0845319792','Mark','Smith'); insert into bkPhone_template values('0828329792','Mickey','Mouse'); insert into bkPhone_template values('0834976958','Bill','Jones'); drop table if exists bkPhone; create table bkPhone like bkPhone_template; alter table bkPhone add unique (phoneNo); insert ignore into bkPhone (phoneNo,firstName,lastName) select phoneNo,firstName,lastName from bkPhone_template; drop table bkPhone_template;
如果数据表已经存在,那么您只需运行create table select,并使用以下insert ignore select.最后,您必须运行一些表重命名语句.就这样.
此解决方法比删除操作快得多.
许多方式导致罗马.这是一.它非常快.所以你可以在大数据库中使用它.别忘了它们.诀窍是:make phoneNo unique并使用"ignore".
drop table if exists bkPhone_template; create table bkPhone_template ( phoneNo varchar(20), firstName varchar(20), lastName varchar(20) ); insert into bkPhone_template values('0783313780','Brady','Kelly'); insert into bkPhone_template values('0845319792','Mark','Smith'); insert into bkPhone_template values('0834976958','Bill','Jones'); insert into bkPhone_template values('0845319792','Mark','Smith'); insert into bkPhone_template values('0828329792','Mickey','Mouse'); insert into bkPhone_template values('0834976958','Bill','Jones'); drop table if exists bkPhone; create table bkPhone like bkPhone_template; alter table bkPhone add unique (phoneNo); insert ignore into bkPhone (phoneNo,firstName,lastName) select phoneNo,firstName,lastName from bkPhone_template; drop table bkPhone_template;
如果数据表已经存在,那么您只需运行create table select,并使用以下insert ignore select.最后,您必须运行一些表重命名语句.就这样.
此解决方法比删除操作快得多.
您可以通过以下方式选择独特的:
select distinct(phoneNo) from bkPhone
并将它们放入另一个表中,删除旧表并将新表重命名为旧名称.