注意:致编辑:如果有更好的标题,请编辑标题:)
我的问题是:
我的数据库中有两个表
-----------
| table1 |
|----------|
| id |
|text |
===========
-----------
| table2 |
|----------|
| id |
|text |
===========
table1 是 600,000 条记录
table2 是 5,000,000 条记录!!:)
删除表2中不在表1中的所有记录的最佳方法是什么
我顺便说一下 - 最快的方法,因为我不想等 4 个小时才能完成这个过程
你有没有比下面的代码更好的东西:
<?PHP
$sql = "select text from table2";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$text = $row["text"];
$sql2 = "select id from table1 where text = '$text'";
$query2 = mysql_query($sql2) or die(mysql_error());
$result2 = mysql_num_rows($query2);
if($result2==0){
$sql3 = "delete from table2 where text = '$text'";
$query3 = mysql_query($sql3) or die(mysql_error());
}
}
?>
谢谢
最佳答案
让 RDBM 处理它怎么样?
例如
DELETE FROM table2 WHERE text NOT IN (select distinct text from table1)
干杯
PS:测试前做一些备份......
关于php - 使用另一个表从表中删除记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1436749/