微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数据库SQL学习的经典案例:学生专业老师分数表的操练

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

MysqL> desc SC;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| S_id  | varchar(10)   | YES  |     | NULL    |       |
| C_id  | varchar(10)   | YES  |     | NULL    |       |
| score | decimal(18,1) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

MysqL> select * from SC;
+------+------+-------+
| S_id | C_id | score |
+------+------+-------+
| 01   | 01   |  80.0 |
| 01   | 02   |  90.0 |
| 01   | 03   |  99.0 |
| 02   | 01   |  70.0 |
| 02   | 02   |  60.0 |
| 02   | 03   |  80.0 |
| 03   | 01   |  80.0 |
| 03   | 02   |  80.0 |
| 03   | 03   |  80.0 |
| 04   | 01   |  50.0 |
| 04   | 02   |  30.0 |
| 04   | 03   |  20.0 |
| 05   | 01   |  76.0 |
| 05   | 02   |  87.0 |
| 06   | 01   |  31.0 |
| 06   | 03   |  34.0 |
| 07   | 02   |  89.0 |
| 07   | 03   |  98.0 |
| 08   | 04   |  79.0 |
| 11   | 03   |  77.9 |
| 12   | 02   |  47.9 |
| 12   | 04   |  47.9 |
| 11   | 01   |  77.9 |
| 01   | 04   |  73.9 |
| 01   | 05   |  83.9 |
| 06   | 04   |  75.0 |
| 06   | 05   |  85.0 |
| 11   | 05   |  81.0 |
| 11   | 04   |  91.0 |
+------+------+-------+
29 rows in set (0.00 sec)


--1
select B.* from SC B where B.C_id = '01' 
and not exists(select * from SC B2 where B.S_id = B2.S_id and B2.C_id = '02');

--2
select * from 
(select B.S_id,B.C_id aC_id,B.score ascore,B2.C_id bC_id,B2.score bscore 
from SC B inner join SC B2 
on B.S_id = B2.S_id and B.C_id = '01' and B2.C_id != '01') 
BBB 
where not exists 
(select * from (select B.S_id,B2.score bscore 
from SC B inner join SC B2 
on B.S_id = B2.S_id and B.C_id = '01' and B2.C_id != '01') 
CCC where BBB.S_id = CCC.S_id and CCC.bC_id = '02' );

--3
select * from SC where C_id = '01' 
and S_id not in 
(select distinct S_id from SC where C_id = '02');

--4
select B.S_id,B2.score bscore 
from SC B inner join SC B2 
on B.S_id = B2.S_id and B.C_id = '01' and B2.C_id != '01' 
group by S_id having (aC_id,bC_id) != ('01','02');

--5
select B.*,B2.* from SC B left join SC B2 
on B.S_id = B2.S_id and B2.C_id = '02' 
where B.C_id = '01' and B2.C_id is null;

--6
select B.*,B2.* from SC B right join SC B2 
on B.S_id = B2.S_id and B.C_id = '02' 
where B2.C_id = '01' and B.C_id is null;

--7
select B.S_id,B2.score bscore 
from SC B inner join SC B2 
on B.S_id = B2.S_id and B.C_id = '01' 
and B2.C_id != '01' 
group by S_id having nullif(bC_id,'02') >> 1;

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐