GROUP分組去除重復數據
/**
* 清除同一專題中重復導入的數據
* @author tanteng
* @date 2014.07.27
*/
public function fuck_repeat(){
set_time_limit(0);
$sql = "SELECT `id` FROM `v95_special_content` GROUP BY `specialid`,`curl` HAVING COUNT(`curl`)>1 ";
$result = $this->db->query($sql);
while ($r = mysql_fetch_assoc($result)) {
$ids[] = $r['id'];
}
$ids = implode(",", $ids);
if (!$ids) {
showmessage('沒有重復數據!',HTTP_REFERER);exit;
}
$sql2 = "DELETE from `v95_special_content` where `id` in($ids) ";
$this->db->query($sql2);
showmessage('執行成功,刪除了'.$this->db->affected_rows().'條重復數據!',HTTP_REFERER);
}
我們分析一下這條sql語句:
SELECT `id` FROM `v95_special_content` GROUP BY `specialid`,`curl` HAVING COUNT(`curl`)>1
這裡根據兩個字段進行了分組,分別是specialid和curl,就是說這兩個字段同時相同的作為條件查詢,having是group語句的條件,相當於where,後面就是計算這樣的條數。
利用mysql的group分組就實現了去除數據庫中多個字段相同的重復數據的操作。
補充:當然還可以使用distinct這個關鍵字來過濾掉多余的重復記錄只保留一條
table
id name
1 a
2 b
3 c
4 c
5 b
select distinct name from table
得到的結果是:
name
a
b
c
好像達到效果了,可是,我想要得到的是id值呢?改一下查詢語句吧
select *, count(distinct name) from table group by name
結果:
id name count(distinct name)
1 a 1
2 b 1
3 c 1