从组中选择后更新记录

时间:2016-04-15 18:02:46

标签: mysql

我选择所有基于任何给定记录的重复记录(而不是简单地选择哪些字段具有重复记录:

select T1.SN,T1.C,T1.S, T2.cnt
from LS T1
join (
  Select SN,C,S,Count(*) as cnt from LS 
where (C in ('M','B','Q','SI','X') and S='N') group by SN having count(*)>1
) as T2 on T1.SN=T2.SN and (T1.C in ('M','B','Q','SI','X') and T1.S='N') 

我想将FieldY更新为' 1'对于这些选定的记录。

1 个答案:

答案 0 :(得分:0)

我试过这个似乎有用,不确定它是否理想:

Update LS as U1
Inner Join (
select T1.ID,T1.SN,T1.C,T1.S, T2.cnt
from LS T1
join (
  Select SN,C,S,Count(*) as cnt from LS 
where (C in ('M','B','Q','SI','X') and S='N') group by SN having count(*)>1
) as T2 on T1.SN=T2.SN and (T1.C in ('M','B','Q','SI','X') and T1.S='N')) as U2 
On U1.ID=U2.ID set FieldY='1'