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

mysqlmysqli database

要删除重复列,请使用 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)

相关文章


有用资源