如何删除巨大的mysql行取决于不相等

时间:2017-04-09 09:16:01

标签: mysql

问题是它的查询量很大

DELETE FROM `members` WHERE `membership` = 'C' AND (`id` != '1' AND `id` !='2' AND ...... thousands of ids );

我该怎么做?

我也做了id NOT IN(“1”,“2”);但也没有工作

我可以使用循环或类似的东西

我要保留并且不删除的ID来自另一个表包含一个字段,其中包含我不想删除的用户ID我使用PHP脚本来帮助我生成SQL查询

喜欢

<?php
require_once("inc.php");
$realty_uids = $db->query("SELECT `uid` from `realty2` ORDER BY `uid`");

$r = $db->fetch_assoc($realty_uids);



while($r = $db->fetch_assoc($realty_uids)){
    $array[] = $r['uid'];
}
$input = array_unique($array);

echo "DELETE from `members` WHERE `membership` = 'C' ";
foreach($input as $key => $value){

    echo " AND `id` != '".$value."' <br>";

}
echo ";";

1 个答案:

答案 0 :(得分:0)

DELETE FROM `members` WHERE `membership` = 'C' AND NOT EXISTS (SELECT at.uid FROM `realty2` at WHERE at.uid = `members`.id);

这种方式非常有效,感谢每一个尝试过的人:)