我找到了一种“删除除 ID 以外的所有重复行分组的重复行”的方法,但它不是纯粹的 MySQL,需要额外的 PHP 代码并且该死的长:
$mysqli = mysqli_connect(...);
function remove_duplicated_rows($table_name) {
global $mysqli;
//get column list
$query = "describe $table_name";
$result = mysqli_query($mysqli,$query);
$rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
$columns = array();
foreach ($rows as $row)
if (strtolower($row["Field"])!="id")
array_push($columns,$row["Field"]);
$column_list = implode(",",$columns);
//create temp table
$temp_table = $table_name."_temporary_table";
$query =
"create table $temp_table as ".
"select * from $table_name where 1 group by $column_list";
mysqli_query($mysqli,$query);
//drop old table
$query = "drop table $table_name";
mysqli_query($mysqli,$query);
//rename temp table to old table
$query = "rename table $temp_table to $table_name";
mysqli_query($mysqli,$query);
}