数据库中的集合操作

create table Student1(
       id int  primary key ,
       stuName varchar(20)
);

insert into Student1 (id,stuName) values(1,'allen');
insert into Student1 (id,stuName) values(2,'vivian');
insert into Student1 (id,stuName) values(3,'joe');
insert into Student1 (id,stuName) values(4,'jason');

create table Course1(
        id int  primary key,
        couName varchar(20)
)
select * from Course1;
insert into Course1 (id,couName) values(1,'math');
insert into Course1 (id,couName) values(2,'chinese');
insert into Course1 (id,couName) values(3,'english');

create table stu_cou(
        id int  primary key,
        stuId int,
        couId int
)
select * from stu_cou;
drop table stu_cou

insert into stu_cou(id,stuId,couId)values(1,1,1);
insert into stu_cou(id,stuId,couId)values(2,1,2);
insert into stu_cou(id,stuId,couId)values(3,1,3);
insert into stu_cou(id,stuId,couId)values(4,2,1);
insert into stu_cou(id,stuId,couId)values(5,2,2);
insert into stu_cou(id,stuId,couId)values(6,2,3);

求Student1中没有上数学课的学生


oracle中集合:

能用集合操作的表的条件:两个表的结构相同,也就是列的个数,列的顺序,列的数据类型一致

合集:union和union all ,其中,union去掉重复,union all不去掉重复,union排序,unoin all 不排序。

交集:intersect

差集:minus


select stuName from Student1 
minus
select stuName from Student1 left join stu_cou on Student1.id = stu_cou.stuid where couId = 1;

数据准确,省略关联了一个表,因为自己写三表关联时出现错误

在网上看三表关联应该是这样写,不用join

select stu.stuName,cou.couName from stu_cou sc ,Student stu,Course1 cou
where  sc.stuId = stu.id
and sc.couId = cou.id

执行可以查询到列,但是数据为空,

用左关联是这样写的:

select stu.stuName,cou.couName from stu_cou sc
left join Student1 stu left join Course1 cou
on sc.stuId = stu.id and sc.couId = cou.id

报 missing key words错误,原因是 一个left jion 配一个on

改为这样:

select stu.stuName,cou.couName from stu_cou sc 
left join Student1 stu on sc.stuId = stu.id
left join Course1 cou on sc.couId = cou.id


就查询出结果了





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值