【学生、教师、专业表相关查询】

作业

现任意数据库中存在表testu、tetea、tema:

testu学生表:
±------±------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------±------------±-----±----±--------±---------------+
| sno | int | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
| tid | int | YES | MUL | NULL | |
±------±------------±-----±----±--------±---------------+
±----±-----±-----+
| sno | name | tid |
±----±-----±-----+
| 1 | 李四 | 1 |
| 2 | 王五 | 2 |
| 3 | 张三 | 3 |
| 4 | 赵码 | NULL |
| 5 | xxxx | 1 |
| 6 | 孙彦 | 2 |
| 7 | 江城 | 1 |
| 8 | 王旭 | 2 |
| 9 | 李四 | 1 |
±----±-----±-----+

testu教师表:
±------±------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------±------------±-----±----±--------±---------------+
| tid | int | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
| major | varchar(10) | YES | | NULL | |
±------±------------±-----±----±--------±---------------+
±----±-------±---------+
| tid | name | major |
±----±-------±---------+
| 1 | 张老师 | English |
| 2 | 王老师 | English |
| 3 | 李老师 | Computer |
±----±-------±---------+

tema专业表
±--------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±--------±------------±-----±----±--------±------+
| major | varchar(10) | YES | | NULL | |
| tuition | mediumtext | YES | | NULL | |
±--------±------------±-----±----±--------±------+
±---------±--------+
| major | tuition |
±---------±--------+
| English | 4688 |
| Computer | 16888 |
±---------±--------+

答:

学生表创建:
create table testu(
sno int(3) primary key auto_increment,
name varchar(10),
tid int not null
);engine innodb default charset=utf8; 

insert into testu (sno,name,tid) values (null,'李四',1);
insert into testu (sno,name,tid) values (null,'王五',2);
insert into testu (sno,name,tid) values (null,'张三',3);
insert into testu (sno,name,tid) values (null,'赵码',null);
insert into testu (sno,name,tid) values (null,'李卓越',1);
insert into testu (sno,name,tid) values (null,'孙彦',2);
insert into testu (sno,name,tid) values (null,'江城',1);
insert into testu (sno,name,tid) values (null,'王旭',2);
insert into testu (sno,name,tid) values (null,'李四',1);
学生表创建:
create table tetea(
tid int primary key auto_increment,
name varchar(10),
major varchar(10)
);engine innodb default charset=utf8;
insert into tetea(tid,name,major) values (null,'张老师','English');
insert into tetea(tid,name,major) values (null,'王老师','English');
insert into tetea(tid,name,major) values (null,'李老师','Computer');
专业表创建:
create table tema(
major varchar(10),
tuition mediumtext
); engine innodb default charset=utf8;
insert into tema (major,tuition) values ('English',4688);
insert into tema (major,tuition) values ('Computer',16888);

1.2、请写出:找出所有研究方向为“Computer”的老师所指导的学生的学号姓名的语句:

select sno,name from testu where tid in(select tid from tetea where major='Computer');

1.3、请写出:找出每个指导老师的姓名、研究方向和有多少名学生的语句:

select sno,name from testu where tid in(select tid from tetea where major='Computer');

1.4、请写出:找出学费(tuition)为4688的课程一共有多少名老师教授,检索出对应专业名字和老师人数,老师人数列命名为“自己姓名_num”:

select tetea.major,count(tetea.major) as 李卓越_num from tetea,tema
 where tetea.major=tema.major and tuition=4688;

1.5、请写出:检索出各位学生姓名和对应指导老师的major,使用至少两种方法:

select t1.name,t2.major from testu as t1,tetea as t2 where t1.tid=t2.tid;
select t1.name,t2.major from testu as t1 left join tetea as t2 on t1.tid=t2.tid;

1.6、请写出:检索出各位学生名字、对应老师名字、专业名字和学费:

select t1.name as '学生',t2.name as '老师',t2.major as '专业',t3.tuition as '学费' from testu as t1,tetea as t2,tema as t3 where t1.tid=t2.tid and t2.major=t3.major;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值