数据库三表嵌套查询

为管理岗位业务培训信息,建立三个表:

 S(S#,SN,SD,SA)S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄

C(C#,CN)C#,CN 分别代表课程编号、课程名称

SC(S#,C#,G)S#,C#,G 分别代表学号、所选修的课程编号、学习成绩

请写出如下 SQL 语句

1、请写出三个表的建表语句

1、create table if not exists `S` (
    -> `id` int(10) not null auto_increment comment 'id',
    -> `S#` int(10) not null comment '学号',
    -> `SN` varchar(10) not null comment '学员姓名',
    -> `SD` varchar(30) not null comment '所属单位',
    -> `SA` int(10) not null comment '学员年龄',
    -> primary key(`id`)
    -> )engine=innoDB default charset=utf8;


2、create table if not exists `C`(
    -> `id` int(10) not null auto_increment comment 'id',
    -> `C#` int(10) not null comment '课程编号',
    -> `CN` varchar(10) not null comment '课程名称',
    -> primary key(`id`)
    -> )engine=innoDB default charset=utf8;


3、create table if not exists `SC`(
    -> `id` int(10) not null auto_increment comment 'id',
    -> `S#` int(10) not null comment '学号',
    -> `C#` int(10) not null comment '课程编号',
    -> `G` int(10) not null comment '学员成绩',
    ->constraint sc_ibfk_1 foreign key (`S#`) references s (`S#`),
    ->constraint sc_ibfk_2 foreign key (`C#`) references c (`C#`)
    -> primary key(`id`)
    -> )engine=innoDB default charset=utf8;

2、使用标准 SQL 嵌套语句查询选修课程名称为‘税收基础’的学员学号和姓名。

select S# ,SN ,
 ->from s,
 ->where S#=(select S# from sc where sc.C#=(select C# from c where CN=税收基础’ ));

3、使用标准 SQL 嵌套语句查询选修课程编号为‘C2’的学员姓名和所属单位。

select SN,SD,
->from s,
->where S#=(select S# from sc where sc.C# in(select C# from c where C#=’C2’));

4、使用标准 SQL 嵌套语句查询选修全部课程的学员姓名和所属单位。

select SN,SD,
->from s,
->where S# in(select S# from sc ,
->right join c,
->no sc.C#=c.C#,
->group by S#,
->having count(*)=count(S#));

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值