检查重复行并更新状态,保持第一个数据不变

时间:2012-11-25 08:48:10

标签: php mysql duplicates

我的桌子示例:

Table_1

+-----id-----+-----zone-----+-----address-----+-----status-----+
=============+==============+=================+================+
     1              5                10               ok
     2              2                 8               ok
     3              3                 2               ok  
     4              2                 5               ok
     5              3                 2               ok 
     6              5                10               ok     
     7              3                 2               ok    

我需要检查重复的条目 [区域/地址组合] 并获得如下表格:

Table_1

+-----id-----+-----zone-----+-----address-----+-----status-----+
=============+==============+=================+================+
     1              5                10               ok
     2              2                 8               ok
     3              3                 2               ok  
     4              2                 5               ok
     5              3                 2               duplicate 
     6              5                10               duplicate 
     7              3                 2               duplicate 

查询应检查区域,地址的重复组合,并应更新“状态”列。 我需要保持第一个重复数据状态不变,其余的重复数据应该标记为“重复”?

关于如何做到这一点的任何想法?

3 个答案:

答案 0 :(得分:2)

你可以用这个:

update
  Table_1 t1 inner join Table_1 t2
  on t1.zone=t2.zone
     and t1.address=t2.address
     and t1.id>t2.id
set t1.status='duplicate'

答案 1 :(得分:0)

我没有测试过,但是这样的事情应该有效。

update table_1 set status = 'duplicate' 
where exists 
select 1 from table_1 t 
where id != t.id
and zone = t.zone
and address = t.address
and t.status = 'ok'

答案 2 :(得分:0)

你可以分两步完成

# set all duplicate rows as "duplicate"
UPDATE Table_1 t1,(SELECT DISTINCT _t1.id,_t1.zone,_t1.address 
                   FROM Table_1 _t1 
                   INNER JOIN Table_1 _t2
                         ON (_t1.address= _t2.address AND _t1.zone = _t2.zone AND _t1.id != _t2.id) 
                   ) t2 
SET t1.status = 'duplicate' WHERE t2.id = t1.id;

# set status = "ok" to the first row with status duplicate 
UPDATE Table_1 t1, (SELECT DISTINCT MIN(_t1.id) AS id FROM Table_1 _t1 
                     WHERE status = "duplicate" 
                     GROUP BY CONCAT(_t1.zone,"|",_t1.address)) t2 
SET t1.status = "ok" WHERE t2.id = t1.id