作者aleck945 (总是不知所措)
看板Database
标题Re: [SQL ] SQL指令请教
时间Wed Jul 9 15:46:05 2008
※ 引述《prima (Underneath Your Clothes)》之铭言:
: delete DEPT
: where ( select count(*)
: from EMP
: where DNO = DEPT.DNO) = 0
: 我有些困惑的是 假设在EMP这个table中,资料如下
: EMPNO NAME DNO JOB
: ===========================
: 001 Lee A1 programmer
: 002 Chen A2 programmer
: 003 Wang A3 programmer
: 004 Chou A3 programmer
: 005 Lai A3 programmer
: 而DEPT此table则是
: DNO DNAMEM LOC
: ==================
: A1 test1 NY
: A2 test2 OH
: A3 test3 CA
: B1 test4 NM
: B2 test5 MI
: B3 test6 LA
: 那( select count(*)
: from EMP
: where DNO = DEPT.DNO) 所得的count值应该是5
: 回到
: delete DEPT
: where ( select count(*)
: from EMP
: where DNO = DEPT.DNO) = 0
: 既然如此那这段delete是怎麽作用的,因为 5 !=0
: 困惑中...
因为 select count(*) from EMP where DNO = DEPT.DNO 这段 subquery 中的 DNO
会由 DEPT 这个 table 依序传给 subquery.
所以会变成:
delete from DEPT
where ( select count(*)
from EMP
where DNO = 'A1') = 0
依序传入 A1 A2 A3 B1 B2 B3 ..
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.116.190.50
1F:推 prima:感谢,了解了:D 07/09 15:53