ORACLE1.14-集合

-- 与数学有关系的集合()

-- 单条语句

select * from t_ke

where ke_name like '%数学%' or ke_name='马克思主义'

 

 

select * from t_ke where ke_name like '%数学%'

union all

select * from t_ke where ke_name='马克思主义'

 

-->>

select * from t_ke where ke_name like '%数学%'

select * from t_ke where ke_name='离散数学'

 

select * from t_ke

where ke_name like '%数学%' or ke_name='离散数学'

 

select * from t_ke where ke_name like '%数学%'

union all

select * from t_ke where ke_name='离散数学'

union all

select 88 id,'城市建设' ke_name from dual

 

 

select * from t_ke where ke_name like '%数学%'

union

select * from t_ke where ke_name='离散数学'

union

select 88 id,'城市建设' ke_name from dual

create table t_stu2(

   id number primary key,

   user_name varchar2(10),

   sex varchar2(10)

);

 

insert into t_stu2 values(1,'小军','男');

insert into t_stu2 values(2,'小黄','女');

insert into t_stu2 values(3,'小陈','男');

insert into t_stu2 values(4,'小姚','女');

commit

 

create table t_yigong(

   id number primary key,

   user_name varchar2(10)

);

 

insert into t_yigong values(1,'小陈');

insert into t_yigong values(2,'小姚');

insert into t_yigong values(3,'小K');

insert into t_yigong values(4,'小T');

commit

select * from t_stu2

select * from t_yigong

 

-- 想看看学生中,有那些是义工

-- 思维1:条件查询

select * from t_stu2

  where user_name in (select user_name from t_yigong)

 

-- 想看看义工中,有那些是学生

-- 思维1:条件查询

select * from t_yigong

  where user_name in (select user_name from t_stu2)

 

 

-- 问题未必看交集的名字,只是我们要用到交集这个概念。

-- 有可能,我查询的内容根本与学生或这义工的名字毫无联系

-- 学生这个群体,愿意当义工的,女孩子多还是男孩子多?

select sex,count(1) from t_stu2 where user_name in(

  select user_name from t_stu2

  intersect

  select user_name from t_yigong

) group by sex

 

insert into t_stu2 values(5,'小冰','女');

insert into t_yigong values(5,'小冰');

Commit

-- 学生这个群体,不愿意当义工的,女孩子多还是男孩子多?

--错误示范(99%大学生会犯的错误)

select sex,count(1) from t_stu2 where user_name not in(

  select user_name from t_stu2

  intersect

  select user_name from t_yigong

) group by sex

--  not in就是一种排除法

-- 学习英语A,B,C,D (要排除3个才能得到正确答案)

-- 对in操作:打开书本,在目录里面查询

-- 对not in操作:打开书本,800页也需要一页一页看

select * from t_stu2

 

select sex,count(1) from t_stu2 where user_name in(

  select user_name from t_stu2

  minus

  select user_name from t_yigong

) group by sex

 

posted @ 2018-03-15 10:19  我喜欢空格键  阅读(87)  评论(0编辑  收藏  举报