sql语句根据另一个故事中的记录删除表1中的行

时间:2013-03-07 16:48:30

标签: sql

我需要表t1和t2。 我想

删除t1中的记录,其中

t1.c1 = t2.c1 AND t1.c2 = t2.c2 AND t1.c3 = t2.c3 AND t2.c4 =“something”

我以为我可以做类似的事情:

delete
FROM table1 t1
INNER JOIN table2 t2
 on 
 (t1.c1 = t2.c1 
  AND t1.c2 = t2.c2 
  AND t1.c3 = t2.c3)
 where t2.c4 = 'something'

但是,在此之前我正在尝试

select *
FROM table1 t1
INNER JOIN table2 t2
 on 
 (t1.c1 = t2.c1 
  AND t1.c2 = t2.c2 
  AND t1.c3 = t2.c3)
 where t2.c4 = 'something' 

并且它没有返回我想要删除的记录。

有关如何修复此查询的任何提示? (在oracle工作)

2 个答案:

答案 0 :(得分:2)

你非常接近!只需在DELETE关键字

之后添加要删除的表名称
DELETE  t1   -- <<==== HERE
FROM    table1 t1
        INNER JOIN table2 t2
            ON  t1.c1 = t2.c1 AND 
                t1.c2 = t2.c2 AND 
                t1.c3 = t2.c3
 WHERE  t2.c4 = 'something'

答案 1 :(得分:0)

我懂了!!

 DELETE  
    FROM    table1 t1
    WHERE EXISTS
      SELECT * 
      from table2 t2
      where  t1.c1 = t2.c1 AND 
            t1.c2 = t2.c2 AND 
            t1.c3 = t2.c3 AND
            t2.c4 = 'something'
相关问题