以下SQL查询允许我删除重复的条目,并使用新ID重新排序数据库中的行。
然而,当删除重复的行时,我需要保留最后一行(并删除所有较旧的副本)。
重复行中的名称,日期和时间相同。定义最新行的唯一标准可能是其ID或作为最后一行存储在数据库中的简单事实。 如何修改代码来修复它?
$creatmp = mysql_query("CREATE TABLE tmp AS SELECT * FROM mytable WHERE 1 GROUP BY name, date, hour, city, province");
$cancmytable = mysql_query("DROP TABLE mytable");
$datmpamytable = mysql_query("ALTER TABLE tmp RENAME TO mytable");
$creareorderid = mysql_query("CREATE TABLE reorderid LIKE mytable");
$popolareorderid = mysql_query("INSERT INTO reorderid SELECT * FROM mytable ORDER BY date,hour");
$emptyfieldid = mysql_query("ALTER TABLE `reorderid` DROP `id`");
$recountone = mysql_query("ALTER TABLE `reorderid` AUTO_INCREMENT = 1");
$recounttwo = mysql_query("ALTER TABLE `reorderid` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;");
$emptymytable = mysql_query("DROP TABLE mytable");
$renamereorderid = mysql_query("ALTER TABLE reorderid RENAME TO mytable");