3 回答
TA贡献1852条经验 获得超1个赞
尝试这样的事情:
$st = $db->query("select id, col1, col2 from teh");
$st->execute();
$arr = $st->fetchAll();
foreach($arr as $el){
$sql = "delete from teh where col1 = acol1 and col2 = acol2 and id <> aid";
$st = $db->prepare($sql);
$st->execute([":acol1" => $el['col1'], ":acol2" => $el['col2'], ":aid" => $el['id']]);
}
这个想法是删除与您当时正在检查的行具有相同值但id值不同的所有行。无需以这种方式连接并将其保存到数组中。
TA贡献1810条经验 获得超5个赞
如果你想删除的行具有较高id但同样col1和col2值,你可以简单地DELETE在那里有匹配的行以较低的行存在id价值
DELETE
t1
FROM teh t1
JOIN teh t2 ON t2.col1 = t1.col1 AND t2.col2 = t1.col2 AND t2.id < t1.id
TA贡献1846条经验 获得超7个赞
尝试这样的事情:
$st = $db->query("select id, col1, col2 from teh");
$st->execute();
$arr = $st->fetchAll();
foreach($arr as $el){
$sql = "delete from teh where col1 = acol1 and col2 = acol2 and id <> aid";
$st = $db->prepare($sql);
$st->execute([":acol1" => $el['col1'], ":acol2" => $el['col2'], ":aid" => $el['id']]);
}
这个想法是删除与您当时正在检查的行具有相同值但id值不同的所有行。无需以这种方式连接并将其保存到数组中。
- 3 回答
- 0 关注
- 197 浏览
添加回答
举报