MySQL,删除重复的行(必须是2列唯一)

Joh*_*ith 2 php mysql

我有这个:

1899, 184, 531 *
1900, 184, 531 *
1901, 113, 531
1902, 184, 436
Run Code Online (Sandbox Code Playgroud)

我想得到

1899, 184, 531
1901, 113, 531
1902, 184, 436
Run Code Online (Sandbox Code Playgroud)

没有临时表等如何?

Tim*_*sen 5

SELECT MIN(Col1), Col2, Col3
FROM yourTable
GROUP BY Col2, Col3
Run Code Online (Sandbox Code Playgroud)

这假设你选择18991900一个记录,因为前者是两者中较小的一个.

如果要从表中删除重复记录(因为已将它们定义为重复记录),则可以使用以下查询:

DELETE t1.*
FROM yourTable t1
LEFT JOIN
(
    SELECT MIN(Col1) AS Col1, Col2, Col3
    FROM yourTable
    GROUP BY Col2, Col3
) t2
    ON t1.Col1 = t2.Col1 AND t1.Col2 = t2.Col2 AND t1.Col3 = t2.Col3
WHERE t2.Col1 IS NULL
Run Code Online (Sandbox Code Playgroud)