mysql,如何删除重复数据?

时间:2022-03-08 09:12:41

i have a table with some duplicate values and i want to remove them:

我有一个表有一些重复的值,我想删除它们:

table1:

表格1:

id   |   access       |   num
12       1144712030       101
13       1144712030       101
14       1154512035       102
15       1154512035       102

i would like to remove the duplicates so i will have left:

我想删除重复项,所以我将离开:

id   |   access       |   num
12       1144712030       101
14       1154512035       102

any idea how to do this in a mysql command?

任何想法如何在mysql命令中执行此操作?

thanks

谢谢

3 个解决方案

#1


2  

The simpler solution i think would be:

我认为更简单的解决方案是:

CREATE TABLE new_table as SELECT id,DISTINCT access,num FROM original_table
TRUNCATE TABLE original_table
INSERT INTO original_table SELECT * FROM new_table
DROP TABLE new_table;

Note:

注意:

I think some kind of cursor could be used, and maybe a temporary table. But you should be really careful.

我认为可以使用某种游标,也许是临时表。但你应该非常小心。

#2


1  

if your table called foo, rename in foo_old, re-create table foo as a structure identical to foo_old. Make a query with the DISTINCT operator obtained and the results reported on Table foo_old enter them in foo.

如果你的表名为foo,在foo_old中重命名,则重新创建表foo作为与foo_old相同的结构。使用获得的DISTINCT运算符进行查询,并在表foo_old上报告的结果在foo中输入它们。

#3


0  

do a quick search here for DELETE DUPLICATE ROWS

在这里快速搜索DELETE DUPLICATE ROWS

you'll find a ton of examples.

你会发现很多例子。

#1


2  

The simpler solution i think would be:

我认为更简单的解决方案是:

CREATE TABLE new_table as SELECT id,DISTINCT access,num FROM original_table
TRUNCATE TABLE original_table
INSERT INTO original_table SELECT * FROM new_table
DROP TABLE new_table;

Note:

注意:

I think some kind of cursor could be used, and maybe a temporary table. But you should be really careful.

我认为可以使用某种游标,也许是临时表。但你应该非常小心。

#2


1  

if your table called foo, rename in foo_old, re-create table foo as a structure identical to foo_old. Make a query with the DISTINCT operator obtained and the results reported on Table foo_old enter them in foo.

如果你的表名为foo,在foo_old中重命名,则重新创建表foo作为与foo_old相同的结构。使用获得的DISTINCT运算符进行查询,并在表foo_old上报告的结果在foo中输入它们。

#3


0  

do a quick search here for DELETE DUPLICATE ROWS

在这里快速搜索DELETE DUPLICATE ROWS

you'll find a ton of examples.

你会发现很多例子。