簡體   English   中英

如何從嵌套查詢的結果中排除名稱?

[英]How do I exclude a name from the results of a nested query?

以下是我的雇員表:

create table EMPLOYEES
(EmpID    char(4)         unique Not null,
 Ename    varchar(10),
 Job      varchar(9),
 MGR      char(4),
 Hiredate date,
 Salary   decimal(7,2),
 Comm     decimal(7,2),
 DeptNo   char(2)         not null,
     Primary key(EmpID),
     Foreign key(DeptNo) REFERENCES DEPARTMENTS(DeptNo));


insert into EMPLOYEES values (7839,'King','President',null,'17-Nov-11',5000,null,10);
insert into EMPLOYEES values (7698,'Blake','Manager',7839,'01-May-11',2850,null,30);
insert into EMPLOYEES values (7782,'Clark','Manager',7839,'02-Jun-11',2450,null,10);
insert into EMPLOYEES values (7566,'Jones','Manager',7839,'02-Apr-11',2975,null,20);
insert into EMPLOYEES values (7654,'Martin','Salesman',7698,'28-Feb-12',1250,1400,30);
insert into EMPLOYEES values (7499,'Allen','Salesman',7698,'20-Feb-11',1600,300,30);
insert into EMPLOYEES values (7844,'Turner','Salesman',7698,'08-Sep-11',1500,0,30);
insert into EMPLOYEES values (7900,'James','Clerk',7698,'22-Feb-12',950,null,30);
insert into EMPLOYEES values (7521,'Ward','Salesman',7698,'22-Feb-12',1250,500,30);
insert into EMPLOYEES values (7902,'Ford','Analyst',7566,'03-Dec-11',3000,null,20);
insert into EMPLOYEES values (7369,'Smith','Clerk',7902,'17-Dec-10',800,null,20);
insert into EMPLOYEES values (7788,'Scott','Analyst',7566,'09-Dec-12',3000,null,20);
insert into EMPLOYEES values (7876,'Adams','Clerk',7788,'12-Jan-10',1100,null,20);
insert into EMPLOYEES values (7934,'Miller','Clerk',7782,'23-Jan-12',1300,null,10);

以下是DEPARTMENTS表:

create table DEPARTMENTS
    (DeptNo   char(2)  unique Not null,
     DName    varchar(14),
     Location varchar(13),
         Primary key(DeptNo));


insert into DEPARTMENTS values (10,'Accounting','New York');
insert into DEPARTMENTS values (20,'Research','Dallas');
insert into DEPARTMENTS Values (30,'Sales','Chicago');
insert into DEPARTMENTS values (40,'Operations','Boston');

以下是我的查詢:

select ename, salary
from EMPLOYEES
where DeptNo in
(select DeptNo
from EMPLOYEES
where Ename ='blake');

如何從結果中排除“布雷克”? 有人告訴我在子查詢中使用NOT IN ,但是它不起作用。

select ename, salary
from EMPLOYEES
where DeptNo in
(select DeptNo
from EMPLOYEES
where Ename ='blake') and Ename <>'blake';

您還可以在第二個查詢中使用not in

SQLFIDDLE演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM