他们都有 fileId 和 userId 字段
表 1:fileId userId
表2:fileId userId
如果它们不在表 1 中,我想删除表 2 中的所有行,基于它们的 fileId 和 userId .. 不仅仅是一个字段,而是两个字段......
亲切的问候,J
delete from table2
where fileid not in (select fileid from table1)
and userId not in (select userId from table1)
看看这个方法。测试工作。
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;
DELETE FROM table2 WHERE userId NOT IN (SELECT userId from table1) AND
fileId NOT IN (SELECT fileId from table1)
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)
)