gpt4 book ai didi

mysql - 删除mysql表中的所有重复项

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:01 25 4
gpt4 key购买 nike

考虑下表。它是从 CSV 导入的,没有主键。

+-----------+----------+----+----+----+
| firstname | lastname | c1 | c2 | c3 |
+-----------+----------+----+----+----+
| johnny | bravo | a | b | c |
| bruce | willis | x | y | x |
| john | doe | p | q | r |
| johnny | bravo | p | q | r |
| johnny | bravo | p | q | r |
| bruce | willis | x | y | z |
+-----------+----------+----+----+----+

我想删除表中 (firstname, lastname) 出现不止一次的所有行。所以输出将是:

+-----------+----------+----+----+----+
| firstname | lastname | c1 | c2 | c3 |
+-----------+----------+----+----+----+
| john | doe | p | q | r |
+-----------+----------+----+----+----+

最佳答案

在 MySQL 中,最好的方法是使用 join:

delete t
from t join
(
select t2.firstname, t2.lastname
from t t2
group by t2.firstname, t2.lastname
having count(*) > 1
) t2
on t.firstname = t2.firstname and
t.lastname = t2.lastname;

关于mysql - 删除mysql表中的所有重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40217558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com