Home > Backend Development > PHP Tutorial > Delete duplicate records in mysql database php code

Delete duplicate records in mysql database php code

WBOY
Release: 2016-07-25 08:58:22
Original
897 people have browsed it
This article introduces an example code that uses PHP to delete duplicate records in the MySQL database. Friends in need can refer to it.

Method: Create a new table and directly import unique data records. The database used in this article is: test_db, and the temporary table is: test_tab. Example:

<?php
/**
* 删除数据库中重复记录
* edit bbs.it-home.org
* at 2013/6/25
*/
set_time_limit(13600);  //设置时间
$conn = mysql_connect('localhost','root',''); //连接数据库
mysql_select_db('test_db',$conn);
mysql_query("set names 'gbk'");
 
$query = mysql_query("select min(id) as mid from my_shuju group by title");  
 
while($row = mysql_fetch_array($query))
 { 
   mysql_query("INSERT into test_tab select id,cid,title,cat,level3,cname from taobaoguanjianci where id=$row[mid]");
 }
?>
Copy after login

Instructions: Pay attention to the usage of the set_time_limit() function. If there is a lot of data, it may time out. Use this function to set the timeout limit.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template