$sql = "select id from store_massage where to_days(now())-to_days(date)>1"
$result = Mysql_query( $sql );
while ($rows = @Mysql_fetch_array (  $result ) ) {
  $delete1 = Mysql_query ("Delete From store_massage Where id = '$rows[id]'");
  $delete2 = Mysql_query ("Delete From restore Where id = '$rows[id]'");}

解决方案 »

  1.   


    $sql = "select id from store_massage where to_days(now())-to_days(date)>1"
    $result = Mysql_query( $sql );
    $Str="";
    while ($rows = @Mysql_fetch_array (  $result ) )
      $Str .="id = '$rows[id]' or ";
    $delete1 = Mysql_query ("Delete From store_massage Where ".$Str." 0;");
    $delete1 = Mysql_query ("Delete From restore Where ".$Str." 0;");
      

  2.   

    这样试试看(我说你动手改代码,呵呵,比较懒)将要删除的文章的id累计写在一个字符串中,比如$delids,格式是这样
    1,5,9,8,12,66
    然后拼成这样一句sql语句
    if($delids!='')
    {
      $sql='delete from restore where id in ('.$delids.')';
      mysql_query($sql);
    }
    这样做的结果就是节省了几乎二分之一的向mysql提交命令的时间。
    事实上,在删除主表记录时最好也采用这种方式,先把满足条件的id累积起来,一次性用in操作符执行删除,而不是检索到一条删一条,这样毫无效率而言的
      

  3.   

    在一个命令中做尽可能多的操作,是尤为重要的。尤其当你有大量的数据,试想想看你有100条记录要删除,按照原来的作法要mysql_query()200次才能删除,现在只要两次就可以。频繁对一个表请求(尤其是写操作)很有可能造成表的死锁,这一直是mysql的痛。