SQL从两个表中的查询匹配条件中删除?

时间:2012-09-10 21:12:41

标签: sql sql-delete

使用此查询删除所有行,而我只想删除两个表中measureid匹配的行,如下所示:

delete from temp1 
where exists (select t1.* 
              from temp1 t1, temp2 t2 
              where t2.measureid = t1.measureid)

我需要改变什么?

2 个答案:

答案 0 :(得分:3)

我认为您不想使用exists我认为您想使用in

delete from temp1 where measureid in 
    (select t1.measureid from temp1 t1, temp2 t2 where t2.measureid = t1.measureid);

或更好

delete from temp1 where measureid in 
    (select measureid from temp2);

答案 1 :(得分:1)

那是因为

select t1.* from temp1 t1, temp2 t2 where t2.measureid = t1.measureid

始终返回行。你可能想要的是

delete from temp1 t1 where exists (select t2.* from temp2 t2 where t2.measureid = t1.measureid)