數(shù)據(jù)庫多表查詢作業(yè)
創(chuàng)建數(shù)據(jù)庫
插入數(shù)據(jù)
mysql> insert into student values(901,'張老大','男',1985,'計(jì)算機(jī)系','北京市海淀區(qū)'),
-> (902,'張老二','男',1986,'中文系','北京市昌平市'),
-> (903,'張三','女',1990,'中文系','湖南省永州市'), -> (904,'李四','男',1990,'英語系','遼寧省阜新市'), -> (905,'王五','女',1991,'英語系','福建省廈門市'),
-> (906,'王六','男',1988,'計(jì)算機(jī)系','湖南省衡陽市');
mysql> insert into score values(null,901,'計(jì)算機(jī)',98),
-> (null,901,'英語',80),
-> (null,902,'計(jì)算機(jī)',65),
-> (null,902,'中文',88),
-> (null,903,'中文',95),
-> (null,904,'計(jì)算機(jī)',70),
-> (null,904,'英語',92),
-> (null,905,'英語',94),
-> (null,906,'計(jì)算機(jī)',90),
-> (null,906,'英語',85);
1.查詢student表的所有記錄
mysql> select * from student;
2.查詢student表的第2條到4條記錄
mysql> select * from student limit 1,3;
3.從student表查詢所有學(xué)生的學(xué)號(hào)(id)、姓名
(name)和院系(department)的信息
mysql> select id as 學(xué)號(hào),name as 姓名,department as 院系 from student;
4.從student表中查詢計(jì)算機(jī)系和英語系的學(xué)生的信息
mysql> select * from student where department='計(jì)算機(jī)系' or department='英語系';
5.從student表中查詢年齡18~22歲的學(xué)生信息
mysql> select *,year(curdate())-birth as age from student where year(curdate())-birth between 18 and 22;
6.從student表中查詢每個(gè)院系有多少人
mysql> select department as 院系,count(*) as 人數(shù) from student group by department;
7.從score表中查詢每個(gè)科目的最高分
mysql> select c_name as 科目,max(grade) as 最高分 from score group by c_name;
8.查詢李四的考試科目(c_name)和考試成績(grade)
mysql> select name as 姓名,c_name as 科目,grade as 成績 from student
-> inner join score on student.id=score.stu_id
-> where name='李四';
9.用連接的方式查詢所有學(xué)生的信息和考試信息
mysql> select * from student iudent inner join score on student.id=score.stu_id;
10.計(jì)算每個(gè)學(xué)生的總成績
mysql> select name as 姓名,sum(grade) as 總成績 from student
-> inner join score on student.id=score.stu_id
-> group by name;
11.計(jì)算每個(gè)考試科目的平均成績
mysql> select avg(grade) from score group by c_name;
12.查詢計(jì)算機(jī)成績低于95的學(xué)生信息
mysql> select * from student
-> inner join (select stu_id,c_name,grade from score where grade<95 and c_name='計(jì)算機(jī)') as stu
-> on student.id=stu.stu_id;
13.查詢同時(shí)參加計(jì)算機(jī)和英語考試的學(xué)生的信息
獲取同時(shí)考計(jì)算機(jī)和英語的學(xué)生ID
select * from score where c_name='英語') as sc2 where sc1.stu_id=sc2.stu_id
根據(jù)ID獲取學(xué)生信息
mysql> select * from student inner join (select sc1.stu_id from (select * from score where c_name='計(jì)算機(jī)') as sc1,(select * from score where c_name='英語') as sc2 where sc1.stu_id=sc2.stu_id) as sc on student.id=sc.stu_id;
14.將計(jì)算機(jī)考試成績按從高到低進(jìn)行排序
mysql> select * from score where c_name='計(jì)算機(jī)' order by grade desc;
15.從student表和score表中查詢出學(xué)生的學(xué)號(hào),然后合并查詢結(jié)果
mysql> select student.id,score.stu_id from student inner join (select distinct stu_id from score) as score on student.id=score.stu_id;
16.查詢姓張或者姓王的同學(xué)的姓名、院系和考試科目及成績
mysql> select stu.name as 姓名, stu.department as 院系, score.c_name as 科目,score.grade as 成績
-> from score inner join
-> (select * from student where name like '張%' or name like '王') as stu
-> on score.stu_id=stu.id;
17.查詢都是湖南的學(xué)生的姓名、年齡、院系和考試科目及成績文章來源:http://www.zghlxwxcb.cn/news/detail-567882.html
mysql> select stu.name as 姓名,year(curdate())-stu.birth as age,stu.department as 院系,score.c_name as 科目,score.grade as 成績
-> from score
-> inner join
-> (select * from student where address like '%湖南%') as stu
-> on score.stu_id=stu.id;
文章來源地址http://www.zghlxwxcb.cn/news/detail-567882.html
到了這里,關(guān)于數(shù)據(jù)庫多表查詢作業(yè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!