找忘记填写性别的员工

已知:

Table:   (员工 emp1)
id   name
1 a
2 b
3 c
4  d
 
Table:( 性别 sext)
 
id sex
1 男
4 女
5 男
 
找出忘记填写性别的员工(用Oracle的两种方式)

Create Table Empl(
  Id Number(4) primary key,
  name varchar2(10)
);

Insert Into Empl Values (1,'a') ;
Insert Into Empl Values (2,'b') ;
Insert Into Empl Values (3,'c') ;
Insert Into Empl Values (4,'d') ;
Insert Into Empl Values (5,'e') ;

Select * From Empl;
/*-----------------------------------------------*/
Create Table sext(
  Id Number(4) Primary Key,
  sex varchar2(10)
);

Insert Into Sext Values(1,'男');
Insert Into Sext Values(3,'女');
Insert Into Sext Values(5,'男');

Select * From Sext;
/*----- 找出忘记填写性别的员工(用Oracle的两种方式) -------------*/

Select Id,Name 
From Empl E 
Where E.Id Not In
(Select Id From Sext); 

--minus差集
Select Id From Empl Minus Select Id From Sext; 

-- <>
Select * From Empl Where Id <> All(Select Id From Sext); 

-- 先求id的差集
Select E.* 
From Empl E,
(Select Id From Empl Minus Select Id From Sext) S /*临时表s*/
Where E.Id = S.Id; 

--左外莲接1
Select E.Id,E.Name 
From Empl E,Sext S 
Where E.Id=S.Id(+) 
And S.Sex Is Null; 
--左外莲接2
Select * From Empl
Left Outer Join Sext 
On Empl.Id = Sext.Id 
Where Sext.Sex Is Null; 

--not exists
Select * From Empl E 
Where Not Exists
    (Select * From Sext S 
     Where E.Id = S.Id); 

--not in
Select * From Empl 
Where Id Not In 
    (Select Empl.Id 
    From Empl, Sext 
    Where Empl.Id = Sext.Id);

--Intersect相交
Select Name From Empl 
Where Id Not In 
    (Select Id From Empl 
    Intersect 
    Select Id From Sext);

--此种先求两个表id的合集。当count(*)=2表示id重复,<2时表示未填写
Select * From Empl E 
Where 
(Select Count(*) From 
    (Select Id From Empl Union All Select Id From Sext) T
Where T.Id=E.Id)<2;


 

你可能感兴趣的:(oracle)