Home  >  Article  >  Backend Development  >  How to remove duplicate data from php database

How to remove duplicate data from php database

藏色散人
藏色散人Original
2021-11-01 09:38:072969browse

How 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

##The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

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 (‘已经没有重复的了’);
}

Recommended learning: "

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!

Statement:
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
Previous article:How to modify sql in phpNext article:How to modify sql in php