删除从同一个表中的选择查询中选择的记录

时间:2014-09-26 19:30:50

标签: sql sql-server oracle oracle-sqldeveloper

我需要删除从子查询中获取的少数记录。

假设EMP表格中包含EMPNAMEEMPSALARY列,而主键是EMPNAME,EMPSALARY的组合。

delete from emp 
where exists (
        select * 
        from EMP ***** //query which fetch few records from EMP table);

以上查询无效。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

你可以直接在where子句中给出条件

delete from emp where <conditions>;

否则你可以这样做

DELETE FROM Table1 T1
WHERE EXISTS (SELECT column1 FROM table2 T2
               WHERE T1.column1 = T2.column1 
                 AND T1.Column2 = T2.column2 );

答案 1 :(得分:1)

您的查询看起来像

delete from emp 
from  emp e
where exists (select * 
              from EMP 
              WHERE e.EMPNAME = EMPNAME
               AND  e.EMPSALARY = EMPSALARY 
               AND  <another Condition>)   --<-- Condition on which you want to delete rows

答案 2 :(得分:0)

您可以使用以下查询:

delete from emp 
where primary_key_column_name IN 
  (
     select primary_key_column_name from EMP where <your conditions>
  );

delete from emp where <your conditions>;