删除匹配多个列相同表的行

时间:2009-06-24 19:00:03

标签: sql

使用SQL,我有5列: ssn,姓氏,名字,RF和标志字段。我需要通过这个表,其中4列等于另一行,并且该行中的flag字段的值等于2050,然后删除该2050记录。

4 个答案:

答案 0 :(得分:8)

delete from MyTable m
where flag = 2050
and exists (
    select 1 from MyTable where
    MyTable.ssn = m.ssn 
        and MyTable.lastname=m.lastname 
        and MyTable.firstname=m.firstname 
        and MyTable.RF=m.RF 
        and MyTable.flag <> 2050
)

答案 1 :(得分:4)

DELETE
     T1
FROM
     My_Table T1
INNER JOIN My_Table T2 ON
     T2.ssn = T1.ssn AND
     T2.last_name = T1.last_name AND
     T2.first_name = T1.first_name AND
     T2.RF_name = T1.RF_name AND
     T2.flag <> T1.flag
WHERE
     T1.flag = 2050

答案 2 :(得分:2)

delete from TableName as tn
where tn.flag = 2050 and
exists (select * from TableName as tn2 where tn.ssn = tn2.ssn
        and tn.lastname = tn2.lastname and tn.firstname = tn2.firstname
        and tn.rf = tn2.rf and tn2.flag <> 2050)

答案 3 :(得分:1)

delete from Table1 
 where flag = 2050 
   and exists (select * 
                 from Table2 
                where ssn = Table1.ssn
                  and lastname = Table1.lastname 
                  and firstname = Table1.firstname 
                  and rf = Table1.rf 
                  and flag <> 2050)