gpt4 book ai didi

php - 删除两列连接重复的行

转载 作者:行者123 更新时间:2023-11-29 01:48:36 25 4
gpt4 key购买 nike

teh - 1400 行。

我想删除 col1col2 重复的行。

例如 - 如果 col1loremcol2ipsum
下一行也是 - col1loremcol2ipsum
next 行应删除。

这是我正在使用的代码,看不出有什么问题,因为最终结果是——只剩下一行,其他的都被删除了。

该表至少有一半关于 col1col2 连接的唯一行。

$st = $db->query("select id, col1, col2 from teh");
$st->execute();
$arr = $st->fetchAll();
$check = [];
foreach($arr as $el){
$str = $el['col1'] . $el['col2'];
if(in_array($str, $check)){
$sql = "delete from teh where id = :aid";
$st = $db->prepare($sql);
$st->execute([":aid" => $el['id']]);
}
else{
array_push($check, $str);
}
}

最佳答案

如果你想删除具有更高 id 但相同 col1col2 值的行,你可以简单地 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

Demo on dbfiddle

关于php - 删除两列连接重复的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57447818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com