需要帮助删除 MySQL 表中的重复列?


要删除重复列,请将 DELETE 与 INNER JOIN 结合使用。以下是语法:

delete tbl1 from yourTableName anyAliasName1
inner join yourTableName anyAliasName2
where
yourCondition1
and
yourCondition2

我们创建一个表:

mysql> create table demo14
−> (
−> id int not null auto_increment primary key,
−> name varchar(30)
−> );
Query OK, 0 rows affected (1.89 sec)

使用 insert 命令在表中插入一些记录:

mysql> insert into demo14(name) values('John');
Query OK, 1 row affected (0.14 sec)

mysql> insert into demo14(name) values('David');
Query OK, 1 row affected (0.18 sec)

mysql> insert into demo14(name) values('David');
Query OK, 1 row affected (0.09 sec)

mysql> insert into demo14(name) values('Bob');
Query OK, 1 row affected (0.15 sec)

mysql> insert into demo14(name) values('John');
Query OK, 1 row affected (0.45 sec)

mysql> insert into demo14(name) values('Carol');
Query OK, 1 row affected (0.16 sec)

使用 select 语句显示表中的记录:

mysql> select *from demo14;

这会产生以下输出:

+----+-------+
| id | name  |
+----+-------+
|  1 | John  |
|  2 | David |
|  3 | David |
|  4 | Bob   |
|  5 | John  |
|  6 | Carol |
+----+-------+
6 rows in set (0.00 sec)

以下是用于从表中删除重复列的查询:

mysql> delete tbl1 from demo14 tbl1
−> inner join demo14 tbl2
−> where
−> tbl1.id < tbl2.id and
−> tbl1.name = tbl2.name
−> ;
Query OK, 2 rows affected (0.20 sec)

使用 select 语句显示表中的记录:

mysql> select *from demo14;

这会产生以下输出:

+----+-------+
| id | name  |
+----+-------+
|  3 | David |
|  4 | Bob   |
|  5 | John  |
|  6 | Carol |
+----+-------+
4 rows in set (0.00 sec)

更新于:19-Nov-2020

362 次浏览

开启您的 职业生涯

完成课程以获取认证

开始
广告