使用MySQL更新语句删除重复
如何在UPDATE语句中使用计数器。例如,我在一列中有一些重复记录,如下所示:使用MySQL更新语句删除重复,mysql,sql-update,Mysql,Sql Update,如何在UPDATE语句中使用计数器。例如,我在一列中有一些重复记录,如下所示: -------------------- Ref_Number -------------------- 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 如何消除重复,以确保结果准确无误 -------------------- Ref_Number -----------------
--------------------
Ref_Number
--------------------
108001798914
108001798914
108001798914
108001798914
108001798914
108001798914
如何消除重复,以确保结果准确无误
--------------------
Ref_Number
--------------------
108001798914
108001798915
108001798916
108001798917
108001798918
108001798919
试试这个。在运行以下操作之前,不要忘记备份数据:
CREATE TABLE temp_table LIKE my_table;
ALTER TABLE temp_table ADD UNIQUE (Ref_Number);
ALTER TABLE temp_table CHANGE Ref_Number Ref_Number INT(10)AUTO_INCREMENT PRIMARY KEY;
INSERT INTO temp_table (all,other,fields,except,refnumber,here)
SELECT all,other,fields,except,refnumber,here FROM my_table
TRUNCATE my_table;
ALTER TABLE my_table ADD UNIQUE (Ref_Number);
ALTER TABLE my_table CHANGE Ref_Number Ref_Number INT(10)AUTO_INCREMENT PRIMARY KEY;
INSERT INTO my_table
SELECT * FROM temp_table
DROP temp_table;
这是示例试试这个。在运行以下操作之前,不要忘记备份数据:
CREATE TABLE temp_table LIKE my_table;
ALTER TABLE temp_table ADD UNIQUE (Ref_Number);
ALTER TABLE temp_table CHANGE Ref_Number Ref_Number INT(10)AUTO_INCREMENT PRIMARY KEY;
INSERT INTO temp_table (all,other,fields,except,refnumber,here)
SELECT all,other,fields,except,refnumber,here FROM my_table
TRUNCATE my_table;
ALTER TABLE my_table ADD UNIQUE (Ref_Number);
ALTER TABLE my_table CHANGE Ref_Number Ref_Number INT(10)AUTO_INCREMENT PRIMARY KEY;
INSERT INTO my_table
SELECT * FROM temp_table
DROP temp_table;
这是示例试试这个。在运行以下操作之前,不要忘记备份数据:
CREATE TABLE temp_table LIKE my_table;
ALTER TABLE temp_table ADD UNIQUE (Ref_Number);
ALTER TABLE temp_table CHANGE Ref_Number Ref_Number INT(10)AUTO_INCREMENT PRIMARY KEY;
INSERT INTO temp_table (all,other,fields,except,refnumber,here)
SELECT all,other,fields,except,refnumber,here FROM my_table
TRUNCATE my_table;
ALTER TABLE my_table ADD UNIQUE (Ref_Number);
ALTER TABLE my_table CHANGE Ref_Number Ref_Number INT(10)AUTO_INCREMENT PRIMARY KEY;
INSERT INTO my_table
SELECT * FROM temp_table
DROP temp_table;
这是示例试试这个。在运行以下操作之前,不要忘记备份数据:
CREATE TABLE temp_table LIKE my_table;
ALTER TABLE temp_table ADD UNIQUE (Ref_Number);
ALTER TABLE temp_table CHANGE Ref_Number Ref_Number INT(10)AUTO_INCREMENT PRIMARY KEY;
INSERT INTO temp_table (all,other,fields,except,refnumber,here)
SELECT all,other,fields,except,refnumber,here FROM my_table
TRUNCATE my_table;
ALTER TABLE my_table ADD UNIQUE (Ref_Number);
ALTER TABLE my_table CHANGE Ref_Number Ref_Number INT(10)AUTO_INCREMENT PRIMARY KEY;
INSERT INTO my_table
SELECT * FROM temp_table
DROP temp_table;
这是示例以下内容将获得新参考号所需的信息——假设与现有参考号没有冲突:
select ref_number,
@rn := if(@refnum = ref_number, @rn + 1, 0) as seqnum,
@refnum = rev_number
from table t cross join
(select @rn := 0, @refnum := -1) const
order by ref_number;
假设您有一个id
列,您可以使用join
将其放入更新中:
update table toupdate join
(select @rn := 0, @refnum := -1, @prev_refnum := -1) const
set ref_number = ref_number +
(case when (@prev_refnum := @refnum) is null then NULL
when (@refnum := ref_number) is null then NULL
when ref_number := @prev_refnum then @rn := @rn + 1
else 0
end)
order by ref_number;
这是一个相当复杂的语句,因为MySQL不便于在update
语句中设置变量。使用案例
只是设置变量以记住以前的值。即使失败,它们也会按顺序执行。以下内容将获得新参考号所需的信息——假设与现有参考号没有冲突:
select ref_number,
@rn := if(@refnum = ref_number, @rn + 1, 0) as seqnum,
@refnum = rev_number
from table t cross join
(select @rn := 0, @refnum := -1) const
order by ref_number;
假设您有一个id
列,您可以使用join
将其放入更新中:
update table toupdate join
(select @rn := 0, @refnum := -1, @prev_refnum := -1) const
set ref_number = ref_number +
(case when (@prev_refnum := @refnum) is null then NULL
when (@refnum := ref_number) is null then NULL
when ref_number := @prev_refnum then @rn := @rn + 1
else 0
end)
order by ref_number;
这是一个相当复杂的语句,因为MySQL不便于在update
语句中设置变量。使用案例
只是设置变量以记住以前的值。即使失败,它们也会按顺序执行。以下内容将获得新参考号所需的信息——假设与现有参考号没有冲突:
select ref_number,
@rn := if(@refnum = ref_number, @rn + 1, 0) as seqnum,
@refnum = rev_number
from table t cross join
(select @rn := 0, @refnum := -1) const
order by ref_number;
假设您有一个id
列,您可以使用join
将其放入更新中:
update table toupdate join
(select @rn := 0, @refnum := -1, @prev_refnum := -1) const
set ref_number = ref_number +
(case when (@prev_refnum := @refnum) is null then NULL
when (@refnum := ref_number) is null then NULL
when ref_number := @prev_refnum then @rn := @rn + 1
else 0
end)
order by ref_number;
这是一个相当复杂的语句,因为MySQL不便于在update
语句中设置变量。使用案例
只是设置变量以记住以前的值。即使失败,它们也会按顺序执行。以下内容将获得新参考号所需的信息——假设与现有参考号没有冲突:
select ref_number,
@rn := if(@refnum = ref_number, @rn + 1, 0) as seqnum,
@refnum = rev_number
from table t cross join
(select @rn := 0, @refnum := -1) const
order by ref_number;
假设您有一个id
列,您可以使用join
将其放入更新中:
update table toupdate join
(select @rn := 0, @refnum := -1, @prev_refnum := -1) const
set ref_number = ref_number +
(case when (@prev_refnum := @refnum) is null then NULL
when (@refnum := ref_number) is null then NULL
when ref_number := @prev_refnum then @rn := @rn + 1
else 0
end)
order by ref_number;
这是一个相当复杂的语句,因为MySQL不便于在update
语句中设置变量。使用案例
只是设置变量以记住以前的值。即使失败,它们也按顺序执行。可能重复的可能重复的可能重复的可能重复的抱歉,我无法解释我的问题。我在表中有很多列,我希望用唯一的记录更新重复的记录。比如使用MAX(Ref_Number)并向其中添加一个,然后更新等等。我的意思是使用唯一的数字。108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798915 108001798916 108001798917 108001798918 108001798919您希望它们是增量的吗?是的,这就是我想要的。递增和更新看起来很接近我想要的。谢谢我现在试一试。对不起,我无法解释我的问题。我在表中有很多列,我希望用唯一的记录更新重复的记录。比如使用MAX(Ref_Number)并向其中添加一个,然后更新等等。我的意思是使用唯一的数字。108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798915 108001798916 108001798917 108001798918 108001798919您希望它们是增量的吗?是的,这就是我想要的。递增和更新看起来很接近我想要的。谢谢我现在试一试。对不起,我无法解释我的问题。我在表中有很多列,我希望用唯一的记录更新重复的记录。比如使用MAX(Ref_Number)并向其中添加一个,然后更新等等。我的意思是使用唯一的数字。108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798915 108001798916 108001798917 108001798918 108001798919您希望它们是增量的吗?是的,这就是我想要的。递增和更新看起来很接近我想要的。谢谢我现在试一试。对不起,我无法解释我的问题。我在表中有很多列,我希望用唯一的记录更新重复的记录。比如使用MAX(Ref_Number)并向其中添加一个,然后更新等等。我的意思是使用唯一的数字。108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798914 108001798915 108001798916 108001798917 108001798918 108001798919您希望它们是增量的吗?是的,这就是我想要的。递增和更新看起来很接近我想要的。谢谢我现在试一试。