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 關注
- 222 瀏覽
添加回答
舉報