php去除数据库里重复记录

去除数据库里重复的记录,说下原理。先找到表中第一条记录,然后根据这条记录去找与这条记录某个字段相同的,但id不同的记录。删除。效率不是很高,但能用

  $start = 0;
 $k = 10000;
 while(1){
       if($k%10000 == 0){
          $con = mysql_connect("localhost","root","root");  //链接数据库 地址:localhost,数据库用户名:getinfo,数据库密码:test123
           mysql_select_db("test");      //选择表名
          mysql_query("set names utf8");        //执行sql语句   设置字符集为UTF8
      }
       $sql = "select * from site_link_3 where lid>$start order by lid asc limit 0,1";
        $result = mysql_query($sql);
        $num = mysql_num_rows($result);
     if($num == 0){
          die('finish');    //退出,       并打印finish
       }
       $info = mysql_fetch_assoc($result);
     mysql_query("delete from site_link_3 where lid!=".$info['lid']." and link='".$info['link']."'");    //去除重复link
      $start = $info['lid'];
        echo $info['lid']."\r\n";
 }

本文为原创,转载请注明出处 83

Comments are closed.