How to remove duplicate data from php database

藏色散人
Release: 2023-03-13 15:42:01
Original
2990 people have browsed it

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

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!

Related labels:
php
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