推 prima:感謝,了解了:D 07/09 15:53
※ 引述《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