当前位置: 首页 > news >正文

多表查询--实例

1
创建student和score表
CREATE TABLE student (
id INT(10) NOT NULL UNIQUE PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
sex VARCHAR(4) ,
birth YEAR,
department VARCHAR(20) ,
address VARCHAR(50)
);
创建score表。SQL代码如下:
CREATE TABLE score (
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,
c_name VARCHAR(20) ,
grade INT(10)
);
2.
为student表和score表增加记录
向student表插入记录的INSERT语句如下:
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
向score表插入记录的INSERT语句如下:
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);
1.查询student表的所有记录
>select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |
| 903 | 张三      | 女   |  1990 | 中文系       | 湖南省永州市       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)

2.查询student表的第2条到4条记录
>select * from score limit 1,3;
+----+--------+-----------+-------+
| id | stu_id | c_name    | grade |
+----+--------+-----------+-------+
|  2 |    901 | 英语      |    80 |
|  3 |    902 | 计算机    |    65 |
|  4 |    902 | 中文      |    88 |
+----+--------+-----------+-------+
3 rows in set (0.00 sec)

注意:limit 1,3 的意思是从1的下一行开始查找,往后找三行。也就是2,3,4行

3.student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
>select id,name,department from student;
+-----+-----------+--------------+
| id  | name      | department   |
+-----+-----------+--------------+
| 901 | 张老大    | 计算机系     |
| 902 | 张老二    | 中文系       |
| 903 | 张三      | 中文系       |
| 904 | 李四      | 英语系       |
| 905 | 王五      | 英语系       |
| 906 | 王六      | 计算机系     |
+-----+-----------+--------------+
6 rows in set (0.00 sec)

4.student表中查询计算机系和英语系的学生的信息
方法1
>select * from student where department='计算机系' or department='英语系';
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.01 sec)方法2
>select * from student where department in ('英语系','计算机系');
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.00 sec)

5.student表中查询年龄18~22岁的学生信息
>select *from student where year(curdate())-birth between 18 and 22;
Empty set (0.00 sec)

6.student表中查询每个院系有多少人

利用数据分组(用department分组)

]>select department,count(department)from student group by department;
+--------------+-------------------+
| department   | count(department) |
+--------------+-------------------+
| 计算机系     |                 2 |
| 中文系       |                 2 |
| 英语系       |                 2 |
+--------------+-------------------+
3 rows in set (0.00 sec)

7.score表中查询每个科目的最高分

利用科目进行分组查询

>select c_name,max(grade) from score group by c_name;
+-----------+------------+
| c_name    | max(grade) |
+-----------+------------+
| 计算机    |         98 |
| 英语      |         94 |
| 中文      |         95 |
+-----------+------------+
3 rows in set (0.00 sec)

8.查询李四的考试科目(c_name)和考试成绩(grade)
内连接查询
>select s.id,s.name,c.c_name,c.grade from student as s inner join score as c ON s.id=c.stu_id where name like '李四';+-----+--------+-----------+-------+
| id  | name   | c_name    | grade |
+-----+--------+-----------+-------+
| 904 | 李四   | 计算机    |    70 |
| 904 | 李四   | 英语      |    92 |
+-----+--------+-----------+-------+
2 rows in set (0.01 sec)

9.用连接的方式查询所有学生的信息和考试信息
]>select * from student as s join score as c ON s.id=c.stu_id ;
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
| id  | name      | sex  | birth | department   | address            | id | stu_id | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |  1 |    901 | 计算机    |    98 |
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |  2 |    901 | 英语      |    80 |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |  3 |    902 | 计算机    |    65 |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |  4 |    902 | 中文      |    88 |
| 903 | 张三      | 女   |  1990 | 中文系       | 湖南省永州市       |  5 |    903 | 中文      |    95 |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |  6 |    904 | 计算机    |    70 |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |  7 |    904 | 英语      |    92 |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |  8 |    905 | 英语      |    94 |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |  9 |    906 | 计算机    |    90 |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       | 10 |    906 | 英语      |    85 |
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
10 rows in set (0.00 sec)

10.计算每个学生的总成绩
>select s.name,sum(c.grade) from student as s inner join score as c on s.id=c.stu_id;
ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'chap01.s.name'; this is incompatible with sql_mode=only_full_group_by
#提示错误:在没有GROUP BY的聚合查询中
>select s.name,sum(c.grade) from 
>student as s inner join score as c
>on s.id=c.stu_id group by s.name;
+-----------+--------------+
| name      | sum(c.grade) |
+-----------+--------------+
| 张老大    |          178 |
| 张老二    |          153 |
| 张三      |           95 |
| 李四      |          162 |
| 王五      |           94 |
| 王六      |          175 |
+-----------+--------------+
6 rows in set (0.00 sec)

11.计算每个考试科目的平均成绩
>select c_name,avg(grade) from score group by c_name;
+-----------+------------+
| c_name    | avg(grade) |
+-----------+------------+
| 计算机    |    80.7500 |
| 英语      |    87.7500 |
| 中文      |    91.5000 |
+-----------+------------+
3 rows in set (0.00 sec)

12.查询计算机成绩低于95的学生信息
>select s.name,c.c_name,c.grade 
>from student as s inner join score as c 
>on s.id=c.stu_id 
>where c.c_name='计算机' andc.grade>=95;
+-----------+-----------+-------+
| name      | c_name    | grade |
+-----------+-----------+-------+
| 张老大    | 计算机    |    98 |
+-----------+-----------+-------+
1 row in set (0.00 sec)

13.查询同时参加计算机和英语考试的学生的信息
14.将计算机考试成绩按从高到低进行排序
]>select c_name,grade from score where c_name='计算机' order by grade desc;
+-----------+-------+
| c_name    | grade |
+-----------+-------+
| 计算机    |    98 |
| 计算机    |    90 |
| 计算机    |    70 |
| 计算机    |    65 |
+-----------+-------+
4 rows in set (0.00 sec)

15.student表和score表中查询出学生的学号,然后合并查询结果
>select s.id from student as s inner join score as c on s.id=c.stu_id;
+-----+
| id  |
+-----+
| 901 |
| 901 |
| 902 |
| 902 |
| 903 |
| 904 |
| 904 |
| 905 |
| 906 |
| 906 |
+-----+
10 rows in set (0.00 sec)

16.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
>select s.name,group_concat(c.c_name),s.department,group_concat(c.grade) 
>from student as s inner join score as c on s..id=c.stu_id 
>where s.name like '王%' or s.name like '张%' group by s.id;
+-----------+------------------------+--------------+-----------------------+
| name      | group_concat(c.c_name) | department   | group_concat(c.grade) |
+-----------+------------------------+--------------+-----------------------+
| 张老大    | 计算机,英语            | 计算机系     | 98,80                 |
| 张老二    | 计算机,中文            | 中文系       | 65,88                 |
| 张三      | 中文                   | 中文系       | 95                    |
| 王五      | 英语                   | 英语系       | 94                    |
| 王六      | 计算机,英语            | 计算机系     | 90,85                 |
+-----------+------------------------+--------------+-----------------------+
5 rows in set (0.00 sec)

17.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
>select s.name,year(curdate())-s.birth as age,s.department,group_concat(c.c_name) ,group_concat(c.grade)from studentas s inne
+--------+------+--------------+------------------------+-----------------------+
| name   | age  | department   | group_concat(c.c_name) | group_concat(c.grade) |
+--------+------+--------------+------------------------+-----------------------+
| 张三   |   33 | 中文系       | 中文                   | 95                    |
| 王六   |   35 | 计算机系     | 计算机,英语            | 90,85                 |
+--------+------+--------------+------------------------+-----------------------+
2 rows in set (0.00 sec)

18.计算学生年龄
>select *,year(curdate())-birth as age from student;
+-----+-----------+------+-------+--------------+--------------------+------+
| id  | name      | sex  | birth | department   | address            | age  |
+-----+-----------+------+-------+--------------+--------------------+------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |   38 |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |   37 |
| 903 | 张三      | 女   |  1990 | 中文系       | 湖南省永州市       |   33 |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |   33 |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |   32 |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |   35 |
+-----+-----------+------+-------+--------------+--------------------+------+
6 rows in set (0.00 sec)

http://www.lryc.cn/news/4386.html

相关文章:

  • Differentially Private Grids for Geospatial Data
  • Java学习记录day8
  • Solon2 开发之容器,三、注入或手动获取 Bean
  • 微信小程序_调用openAi搭建虚拟伙伴聊天
  • 硬件工程师入门基础知识(一)基础元器件认识(一)
  • TCP的运输连接管理
  • 地级市用电、用水、用气数据指标
  • 安装deepinlinuxV20.8配置docker和vscode开发c语言
  • java08-面向对象3
  • 【Spark分布式内存计算框架——Spark Core】8. 共享变量
  • C++多态常见面试题
  • 字母板上的路径 题解,力扣官方出来挨打(小声)
  • 代码随想录算法训练营第二十六天 | 39. 组合总和,40.组合总和II,131.分割回文串
  • vueday01-脚手架安装详细
  • 初识cesium3d(一)
  • 点云转3D网格【Python】
  • 【OpenCV图像处理系列一】OpenCV开发环境的安装与搭建(Ubuntu + Window都适用)
  • 【代码随想录】-动态规划专题
  • c++数据类型 输入输出
  • 【设计模式-11】责任链模式
  • SpringBoot+Vue实现智能物流管理系统
  • 【MT7628】MT7628如何修改串口波特率、调试串口物理口、使用UART3口
  • css盒模型介绍
  • onetab 谷歌插件历史数据清除
  • GRBL源码简单分析
  • 第一部分:简单句——第一章:简单句的核心——二、简单句的核心变化(谓语动词的情态)
  • 软考高级考试中有五大证书,其中哪个更值得考?
  • FlexRay™ 协议控制器 (E-Ray)-04
  • container_of 根据成员变量获得包含其的对象的地址!
  • Linux进程概念