日期:2014-05-17  浏览次数:20857 次

需要写一个SQL 实现两个表的查询
TEBLE AAA 

ID NAME DEPT
1 A 01 
2 B 01
3 C 03




TEBLE BBB

DEPT DEPTNAME
01 111
03 222

要求删除和A不为同一个部门的AAA表中的数据

也就是删除C的记录 但是用SQL怎么写呀



------解决方案--------------------
delete from aaa where dept not in (select dept from aaa where name = 'A')
------解决方案--------------------
SQL code
create table aaa(ID int,NAME varchar2(10),DEPT varchar2(10))

insert into aaa values(1 ,'A', '01')  

insert into aaa values(2 ,'B', '01')

insert into aaa values(3 ,'C', '03')


delete from aaa where dept not in (select dept from aaa where name = 'A')

select * from aaa

/*
        ID NAME       DEPT      
---------- ---------- ----------
         1 A          01        
         2 B          01        

2 rows selected.
*/

------解决方案--------------------
SQL code

delete from AAA
where DEPT <> '01'; --这个01 你可以通过查询得出,但是根据你的描述写死也是可以的。

--不写死
delete from AAA
where DEPT <>(select DEPT from dept where NAME = 'A');