sql - 使用内部联接更新记录

时间:2015-11-03 09:32:36

标签: sql sql-server sql-update inner-join

我正在使用系统,我必须根据4列更新字段 dupstat 以获取重复记录,即 cname,fname,mname ,dob 。另外,我必须检查 资格 列是否包含值“否”。我有以下查询来查找重复记录:

select o.cname, o.fname, o.CRollNo, o.Coll_Code, o.mname, o.dob, oc.dupeCount, o.Eligible, o.dupstat
from REGN_Temp o
inner join (
    SELECT cname, COUNT(*) AS dupeCount, FName, mname, dob
    FROM REGN_Temp
    GROUP BY cname, FName, mname,dob
    HAVING COUNT(*) > 1
) oc on o.cname = oc.cname and o.fname = oc.FName and o.mname = oc.mname and o.dob=oc.dob and o.Eligible='No' order by cname, fname

现在,我想更新所有重复且 资格 的记录的“ dupstat = Y”列strong> =“NO”

1 个答案:

答案 0 :(得分:4)

您可以JOIN直接使用UPDATE,只需使用SELECT代替SET,然后使用UPDATE o SET o.dupstat='Y' from REGN_Temp o inner join ( SELECT cname, COUNT(*) AS dupeCount, FName, mname, dob FROM REGN_Temp GROUP BY cname, FName, mname,dob HAVING COUNT(*) > 1 ) oc on o.cname = oc.cname and o.fname = oc.FName and o.mname = oc.mname and o.dob=oc.dob and o.Eligible='No' ,如下所示:

$_POST = array_map('sanitize', $_POST);