原創文章引自(www.ympfb.com/show-29-64-1.html)
開發專案的時候往往會有重複資料,如何去除重複資料呢?今天給大家分享一個簡單的方法,
正常無論是用框架開發還是原生php都很少有自帶的去重複的方法,基本上都需要我們自己嵌入原生sql,下面直接給大家上原始碼
//原生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(‘已經沒有重複的了’);
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結