データベースの複数テーブルのクエリ ジョブ

データベースの複数テーブルのクエリ ジョブ

ここに画像の説明を挿入
ここに画像の説明を挿入
ここに画像の説明を挿入
データベースを作成して
ここに画像の説明を挿入
データを挿入する

mysql> insert into student values(901,'张老大','男',1985,'计算机系','北京市海淀区'),
    -> (902,'张老二','男',1986,'中文系','北京市昌平市'),
    -> (903,'张三','女',1990,'中文系','湖南省永州市'),                                           -> (904,'李四','男',1990,'英语系','辽宁省阜新市'),                                           -> (905,'王五','女',1991,'英语系','福建省厦门市'),
    -> (906,'王六','男',1988,'计算机系','湖南省衡阳市');

mysql> insert into score values(null,901,'计算机',98),
    -> (null,901,'英语',80),
    -> (null,902,'计算机',65),
    -> (null,902,'中文',88),
    -> (null,903,'中文',95),
    -> (null,904,'计算机',70),
    -> (null,904,'英语',92),
    -> (null,905,'英语',94),
    -> (null,906,'计算机',90),
    -> (null,906,'英语',85);

ここに画像の説明を挿入
1. Student テーブルのすべてのレコードをクエリします

mysql> select * from student;

ここに画像の説明を挿入

2. Student テーブルの 2 番目から 4 番目のレコードをクエリします

mysql> select * from student limit 1,3;

ここに画像の説明を挿入

3. 学生テーブルから全学生の学生番号(id)、名前
(name)、学部(学科)情報を照会します。

mysql> select id as 学号,name as 姓名,department as 院系 from student;

ここに画像の説明を挿入

4. 学生テーブルからコンピュータ科と英語科の学生の情報を問い合わせる

mysql> select * from student where department='计算机系' or department='英语系';

ここに画像の説明を挿入

5. 学生テーブルから18~22歳の学生の情報を問い合わせる

mysql> select *,year(curdate())-birth as age from student where year(curdate())-birth between 18 and 22;

ここに画像の説明を挿入

6. 学生テーブルから各部門に何人いるかをクエリします。

mysql> select department as 院系,count(*) as 人数 from student group by department;

ここに画像の説明を挿入

7. スコアテーブルから各科目の最高スコアをクエリします。

mysql> select c_name as 科目,max(grade) as 最高分 from score group by c_name;

ここに画像の説明を挿入

8. Li Si の被験者 (c_name) とテスト結果 (成績) を照会します。

mysql> select name as 姓名,c_name as 科目,grade as 成绩 from student
    -> inner join score on student.id=score.stu_id
    -> where name='李四';

ここに画像の説明を挿入

9. 接続してすべての学生情報とテスト情報を照会する

mysql> select * from student iudent inner join score on student.id=score.stu_id;

ここに画像の説明を挿入

10. 各生徒の合計成績を計算します

mysql> select name as 姓名,sum(grade) as 总成绩 from student
    -> inner join score on student.id=score.stu_id
    -> group by name;

ここに画像の説明を挿入

11. 各試験科目の平均点を計算します。

mysql> select avg(grade) from score group by c_name;

ここに画像の説明を挿入

12. コンピューターのスコアが 95 未満の生徒の情報を照会する

mysql> select * from student
    -> inner join (select stu_id,c_name,grade from score where grade<95 and c_name='计算机') as stu
    -> on student.id=stu.stu_id;

ここに画像の説明を挿入

13. コンピュータ試験と英語試験の両方を受ける学生の情報を照会する

パソコンと英語試験の学生証を同時に取得

select * from score where c_name='英语') as sc2 where sc1.stu_id=sc2.stu_id

IDで学生情報を取得

mysql> select * from student inner join (select sc1.stu_id from (select * from score where c_name='计算机') 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. コンピューター テストのスコアを高いものから低いものに並べ替える

mysql> select * from score where c_name='计算机' order by grade desc;

ここに画像の説明を挿入

15. 学生テーブルとスコアテーブルから学生 ID 番号をクエリし、クエリ結果をマージします。

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. Zhang または Wang という姓の学生の名前、学部、試験科目、成績を問い合わせる

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. 湖南省出身の学生の名前、年齢、学科、試験科目、成績を問い合わせる

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;

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_55822200/article/details/131707252