##The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computerHow to remove duplicate data from PHP database: 1. Open the corresponding PHP file; 2. Connect to the database; 3. Use the native SQL statement "Db::execute('DELETE from data WHERE (phone) in.. ." Just remove duplicate data.
How to remove duplicate data from PHP database?
When developing projects, there is often duplicate data. How to remove duplicate data?Today I will share with you a simple method,Normally, whether it is developed using a framework or native PHP, there are few built-in deduplication methods. Basically, we need to embed the native SQL ourselves. The source code will be directly uploaded to you below
// 原生 sql 去重只留一条 $res = Db::execute(‘DELETE from data WHERE (phone) in (SELECT phone from (SELECT phone FROM data GROUP BY phone HAVING COUNT(phone)>1) s1) AND id NOT in (SELECT id from (SELECT id FROM data GROUP BY phone HAVING COUNT(phone)>1) s2) ‘); if($res){ $this->success (‘去重成功’, ‘index/maamiya’); }else{ $this->error (‘已经没有重复的了’); }
PHP Video Tutorial"
The above is the detailed content of How to remove duplicate data from php database. For more information, please follow other related articles on the PHP Chinese website!