如何使用LEFT JOIN从MySQL表中删除重复值?
让我们首先创建一个表-
mysql> create table DemoTable(
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
FirstName varchar(100)
);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(FirstName) values('Chris');mysql> insert into DemoTable(FirstName) values('Robert');
mysql> insert into DemoTable(FirstName) values('Robert');
mysql> insert into DemoTable(FirstName) values('John');
mysql> insert into DemoTable(FirstName) values('John');
mysql> insert into DemoTable(FirstName) values('Mike');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-----------+| Id | FirstName |
+----+-----------+
| 1 | Chris |
| 2 | Robert |
| 3 | Robert |
| 4 | John |
| 5 | John |
| 6 | Mike |
+----+-----------+
6 rows in set (0.00 sec)
以下是从MySQL表中删除重复值的查询-
mysql> delete tbl from DemoTable tblleft join(
select min(Id) as Id, FirstName
from DemoTable
group by FirstName
) tbl1 ON tbl.Id = tbl1.Id AND tbl.FirstName = tbl1.FirstName
where tbl1.Id IS NULL;
让我们再次检查表记录。
mysql> select *from DemoTable;
这将产生以下输出-
+----+-----------+| Id | FirstName |
+----+-----------+
| 1 | Chris |
| 2 | Robert |
| 4 | John |
| 6 | Mike |
+----+-----------+
4 rows in set (0.00 sec)
以上是 如何使用LEFT JOIN从MySQL表中删除重复值? 的全部内容, 来源链接: utcz.com/z/321586.html