[SQL ] SQL指令請教

看板Database作者 (Underneath Your Clothes)時間17年前 (2008/07/09 15:13), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/2 (看更多)
最近在上database的課,上課有個SQL指令的意思有點摸不著頭緒 能否有版友幫忙解釋一下? 假設我有兩個Table: EMP和DEPT 1.EMP(EMPNO, NAME, DNO, JOB, MGR, SAL) PK是EMPNO, DNO是reference到EMP這個table的FK 2.DEPT(DNO, DNAMEM LOC) PK是DNO 問題一:從DEPT這個table刪除沒有員工的部門的資料 (delete from the DEPT table the departments having no employees) 上課的slide答案是 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 困惑中... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 164.107.92.125

07/09 18:21, , 1F
我很好奇為何要用count(*)=0, 而不用not exists @_@
07/09 18:21, 1F
文章代碼(AID): #18T6Ki5Z (Database)
文章代碼(AID): #18T6Ki5Z (Database)