Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在保留至少一行的同时删除重复的MySQL表行_Mysql - Fatal编程技术网

如何在保留至少一行的同时删除重复的MySQL表行

如何在保留至少一行的同时删除重复的MySQL表行,mysql,Mysql,我有四列,都是varcharx DealerId 名字 姓氏 街道地址 然后我有一个自动递增的int列,叫做SystemId 我想做的是删除表中4列匹配的所有行,但我想保留自动递增id最大的行 比如说 SystemId|DealerId|FirstName|LastName|StreetAddress 1|Hello|Mike|Tola|1923 somewhere dr 2|Hello|Mike|Tola|1923 somewhere dr 在上面的示例中,我想删除SystemId 1并保留

我有四列,都是varcharx

DealerId 名字 姓氏 街道地址 然后我有一个自动递增的int列,叫做SystemId

我想做的是删除表中4列匹配的所有行,但我想保留自动递增id最大的行

比如说

SystemId|DealerId|FirstName|LastName|StreetAddress
1|Hello|Mike|Tola|1923 somewhere dr
2|Hello|Mike|Tola|1923 somewhere dr
在上面的示例中,我想删除SystemId 1并保留SystemId 2。我有大约30万行,因此无法逐个获取这些id。

您可以使用连接:

您可以使用联接来执行此操作:

这应该行得通

delete from table_name
where SystemId IN
(select t1.SystemId from table_name t1, table_name t2 where t1.DealerId=t2.DealerId and t1.firstname=t2.firstname and t1.lastname=t2.lastname and t1.streetaddress=t2.streetaddress and t1<t2  )
这应该行得通

delete from table_name
where SystemId IN
(select t1.SystemId from table_name t1, table_name t2 where t1.DealerId=t2.DealerId and t1.firstname=t2.firstname and t1.lastname=t2.lastname and t1.streetaddress=t2.streetaddress and t1<t2  )

您可以使用直接连接:

delete t2
from mytable t
join mytable t2 on t.dealerid = t2.dealerid
  and t.firstname = t2.firstname
  and t.lastname = t2.lastname
  and t.streetaddress = t2.streetaddress
  and t.systemid > t2.systemid


诀窍是比较t.systemid>t2.systemid,它保持最高的最后添加的id。

您可以使用一个简单的连接:

delete t2
from mytable t
join mytable t2 on t.dealerid = t2.dealerid
  and t.firstname = t2.firstname
  and t.lastname = t2.lastname
  and t.streetaddress = t2.streetaddress
  and t.systemid > t2.systemid


诀窍是比较t.systemid>t2.systemid,它保持最后添加的最高id。

这里是另一种方法,在最大systemid上使用连接

DELETE t
FROM mytable t
LEFT JOIN 
(
SELECT MAX(SystemId) SystemId
  FROM mytable
 GROUP BY DealerId,FirstName,LastName,StreetAddress

) t2
ON(t.SystemId = t2.SystemId)
WHERE t2.SystemId IS NULL

下面是另一种方法,使用最大SystemId上的联接

DELETE t
FROM mytable t
LEFT JOIN 
(
SELECT MAX(SystemId) SystemId
  FROM mytable
 GROUP BY DealerId,FirstName,LastName,StreetAddress

) t2
ON(t.SystemId = t2.SystemId)
WHERE t2.SystemId IS NULL

如果在四列上有一个索引,那么它的性能应该比我的解决方案更好。如果没有索引和大量要删除的行,我的解决方案可能会执行得更好。如果在四列上有索引,这应该比我的解决方案执行得更好。如果没有索引和大量要删除的行,我的解决方案可能会执行得更好。