要删除重复的列,请将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) −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo14(name) values('John'); mysql> insert into demo14(name) values('David'); mysql> insert into demo14(name) values('David'); mysql> insert into demo14(name) values('Bob'); mysql> insert into demo14(name) values('John'); mysql> insert into demo14(name) values('Carol');
使用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 −> ;
使用select语句显示表中的记录-
mysql> select *from demo14;
这将产生以下输出-
+----+-------+ | id | name | +----+-------+ | 3 | David | | 4 | Bob | | 5 | John | | 6 | Carol | +----+-------+ 4 rows in set (0.00 sec)