如果不在表1中,则从表2中删除,基于2个字段

时间:2012-07-10 10:25:06

标签: php mysql

他们都有fileId和userId字段

表1: fileId userId

表2: fileId userId

我想删除表2中的所有行,如果它们不在表1中,基于它们的fileId和userId ..不仅仅是一个字段,而是在两个字段上......

亲切的问候, J

4 个答案:

答案 0 :(得分:1)

delete from table2 
where fileid not in (select fileid from table1)
and userId not in (select userId from table1)

答案 1 :(得分:0)

TRY(假设id是两个表的主键)

DELETE FROM table2
WHERE id NOT IN ( 
        SELECT 'id' FROM table1 t1 
        INNER JOIN table2 t2 ON ( t1.field=t2.filed AND t1.userid = t2.userid)
        )

答案 2 :(得分:0)

DELETE FROM table2 WHERE userId NOT IN (SELECT userId from table1) AND 
                         fileId NOT IN (SELECT fileId from table1)

答案 3 :(得分:0)

查看这种方法。经过测试的工作。

CREATE table table1(field varchar(10),userid varchar(20));
CREATE table table2(field varchar(10),userid varchar(20));

insert into table1 values('10','vish');
insert into table1 values('11','atul');
insert into table1 values('12','nish');

insert into table2 values('10','vish');
insert into table2 values('11','atul');
insert into table2 values('13','paul');
insert into table2 values('14','ganesh');

DELETE from table2
WHERE NOT exists 
(select field+userid from table1 t1 where t1.field+userid = table2.field+userid);

SELECT * FROM table2;