0

我的表示例:

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 

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

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

4

3 回答 3

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'
于 2012-11-25T09:34:30.040 回答
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'
于 2012-11-25T08:56:28.693 回答
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 
于 2012-11-25T09:21:08.460 回答