我有四列,所有列都是varchar(x)
。
然后我有一个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
。我有大约300k
行,所以一个接一个地抓住这些id是不可能的。
答案 0 :(得分:3)
您可以使用直接加入:
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
请参阅SQL Fiddle
技巧是t.systemid > t2.systemid
的比较,它保持最高(最后添加)id。
答案 1 :(得分:1)
您可以使用join
:
delete t
from table t join
(select t2.dealerid, t2.firstname, t2.lastname, t2.streetaddress,
max(t2.systemId) as maxsystemid
from table t2
group by t2.dealerid, t2.firstname, t2.lastname, t2.streetaddress
) 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.maxsystemid;
答案 2 :(得分:1)
这应该有效
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 )
答案 3 :(得分:1)
以下是使用最大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