为管理岗位业务培训信息建立3个表。
S (S_ID, SN, SD, SA) S_ID,SN,SD,SA分别代表学号,学生姓名,学生单位,学生年龄。
C (C_ID,CN) C_ID,CN分别代表课程编号,课程名称。
SC (S_ID,C_ID,G) S_ID,C_ID,G分别代表学号,课程号,成绩。
1. 请用SQL查询选修了课程名称为“税收基础”的学生学号及姓名。
2. 查询选修了’C2’课程的学员数。
3. 查询选修课程超过5门的学生学号及单位。
4. 请用sql语句查询选修了所有课程的学生姓名和所属单位。
5. 查询选修了课程的学员数。
解答如下:
第一步,先建表:
create table S(
S_ID number(10) primary key,
SN varchar2(30),
SD varchar2(30),
SA number(10)
)
create table C(
C_ID number(10) primary key,
CN varchar2(30)
)
create table SC(
S_ID number(10),
C_ID number(10),
G number(10),
foreign KEY(S_ID) REFERENCES S(S_ID),
foreign key(C_ID) references C(C_ID)
)
commit;
第二步:像各个表中插入数据
insert into S values(20084001, '张飞飞', '蜀国',35);
insert into S values(20084002, '刘备备', '蜀国', 35);
insert into S values(20084003, '诸葛葛', '蜀国', 35);
insert into S values(20084004, '关羽羽', '蜀国', 35);
insert into S values(20084005, '孙权权', '吴国', 35);
insert into S values(20084006, '周瑜瑜', '吴国', 35);
insert into S values(20084007, '曹操操', '魏国', 35);
insert into S values(20084008, '曹丕丕', '魏国', 35);
insert into S values(20084009, '曹仁仁', '魏国', 35);
insert into S values(200840010, '庞统统', '蜀国', 35);
insert into C values(00000001, '语文');
insert into C values(00000002, '英语');
insert into C values(00000003, '物理');
insert into C values(00000004, '计算机');
insert into C values(00000005, 'JAVA');
insert into C values(00000006, 'C++');
insert into C values(00000007, '编译原理');
insert into C values(00000008, '软件工程');
insert into C values(00000009, '数据结构');
insert into C values(000000010, '数据库');
insert into C values(000000011, '税收基础');
insert into SC values(20084001, 00000000, 89);
insert into SC values(20084001, 00000002, 99);
insert into SC values(20084001, 00000006, 65);
insert into SC values(20084001, 00000007, 49);
insert into SC values(20084001, 00000008, 49);
insert into SC values(20084001, 00000009, 67);
insert into SC values(20084002, 00000000, 99);
insert into SC values(20084005, 00000007, 66);
insert into SC values(20084005, 00000009, 10);
insert into SC values(20084006, 00000000, 89);
insert into SC values(20084006, 00000001, 89);
insert into SC values(20084006, 00000002, 76);
insert into SC values(20084006, 00000003, 89);
insert into SC values(20084006, 00000004, 89);
insert into SC values(20084006, 00000005, 89);
insert into SC values(20084006, 00000006, 89);
insert into SC values(20084006, 00000007, 89);
insert into SC values(20084006, 00000008, 89);
insert into SC values(20084006, 00000009, 89);
insert into SC values(20084006, 000000010, 89);
insert into SC values(20084006, 000000011, 89);
insert into SC values(20084008, 000000011, 85);
第1题:
方法一:分三步进行
Select c_id
From C
Where CN='税收基础';(--使C_ID变成已知)
Select s_id
From SC
Where C_id in (Select c_id
From C
Where CN='税收基础');(--根据SC表结合已知的C_ID得到S_ID)
Select S_ID , SN
From S
Where S_ID in (Select s_id
From SC
Where C_id in (Select c_id
From C
Where CN='税收基础'));(--根据S表结合已知的S_ID查询出S_ID、SN)
方法二(根据方法一很容易理解方法二,菜鸟轻松掌握左连接!左外连接就是以左表为准,去匹配右表,左表有多少条数据,结果就是多少条数据):
Select s.s_id, s.SN
From S(方法一的第三步)
Left join SC ON s.s_id = SC.s_id(方法一的第二步:得到S_ID)
Left join C on SC.C_id = C.C_ID
Where C.CN='税收基础';(方法一的第一步:得到C_ID)
第2题:
select count(distinct S_ID) from C,SC (选择这样的对象的个数(即行数))
where C.C_ID=SC.C_ID and C.CN = 'C2'(这样的对象满足的条件)
第3题:
Select S_ID , SD
From S where S_ID in (
Select S_ID from (
Select S_ID, count(distinct C_ID) c_cnt
From SC
Group by S_ID
)(新表(S_ID, c_cnt))
Where c_cnt > 5)
第4题:
select SN, SD
from S where S_ID in (
select S_ID from (
select S_ID , count(distinct C_ID) c_cnt
from SC group by S_ID
)
where c_cnt = (select count(distinct C_ID) from C)
);
第5题:
select count(distinct S_ID) from SC
- 3楼han_yankun2009昨天 15:51
- 学习中,加油
- Re: liumeiqqzj昨天 15:52
- 呵回复han_yankun2009
- 2楼hejingyuan6昨天 11:27
- 正在学习,加油
- Re: liumeiqqzj昨天 15:51
- 呵呵回复hejingyuan6
- 1楼SmithLiu328昨天 10:00
- pingback from :http://blog.csdn.net/SmithLiu328
- Re: liumeiqqzj昨天 10:03
- 很好!回复SmithLiu328