整理各种查询语句,需要查询数据请参考:

https://blog.csdn.net/chennuan1991/article/details/105240457

基础查询语句,请参考:

https://blog.csdn.net/chennuan1991/article/details/105250429
https://blog.csdn.net/chennuan1991/article/details/105255954

多层嵌套查询

查询‘李想’教师任课的学生成绩

步骤1:查询李想的教师编号

mysql> select tno from teacher where tname ='李想';
+-----+
| tno |
+-----+
| 804 |
+-----+
1 row in set (0.00 sec)

步骤2:根据教师编号tno查询出所教课程的编号cno

mysql> select cno from course where tno =(select tno from teacher where tname ='李想');
+-------+
| cno   |
+-------+
| 3-245 |
+-------+
1 row in set (0.00 sec)

步骤3:根据tno,cno查询学生成绩

mysql> select * from score where cno = (select cno from course where tno =(select tno from teacher where tname ='李想'));
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |     86 |
| 105 | 3-245 |     75 |
| 109 | 3-245 |     68 |
+-----+-------+--------+
3 rows in set (0.00 sec)

函数

year()函数

查询和学号为108、101的同学同年出生的所有学生的sno,sname,birthday列

步骤1:查询108、101同学的出生年份

mysql> select year(birthday) from students where sno in(108,101);
+----------------+
| year(birthday) |
+----------------+
|           1977 |
|           1975 |
+----------------+
2 rows in set (0.00 sec)

步骤2:查询同年学生的信息

mysql> select sno,sname,birthday from students where year(birthday) in(select year(birthday) from students where sno in(108,101));
+-----+--------+---------------------+
| sno | sname  | birthday            |
+-----+--------+---------------------+
| 101 | 张三   | 1977-09-01 00:00:00 |
| 102 | 李四   | 1975-10-02 00:00:00 |
| 105 | 小丽   | 1975-02-10 00:00:00 |
| 108 | 大妞   | 1975-02-10 00:00:00 |
+-----+--------+---------------------+
4 rows in set (0.00 sec)

count()函数

查询95031班学生人数

mysql> select count(*) from students where class = '95031';
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

一、count情况

1、count(1):可以统计表中所有数据,不统计所有的列,用1代表代码行,在统计结果中包含列字段为null的数据;

2、count(字段):只包含列名的列,统计表中出现该字段的次数,并且不统计字段为null的情况;

3、count(*):统计所有的列,相当于行数,统计结果中会包含字段值为null的列;

二、count执行效率

列名为主键,count(列名)比count(1)快;列名不为主键,count(1)会比count(列名)快;

如果表中多个列并且没有主键,则count(1)的执行效率优于count(*);

如果有主键,则select count(主键)的执行效率是最优的;如果表中只有一个字段,则select  count(*)最优。

any()

查询选修编号为3-105 课程,且成绩至少高于选修编号为3-245的同学的cno,sno,degree,成绩由高到低排序

步骤1:查询3-245课程的成绩

mysql> select degree from score where cno = '3-245';
+--------+
| degree |
+--------+
|     86 |
|     75 |
|     68 |
+--------+
3 rows in set (0.00 sec)

 步骤2:查询选修编号为3-105 课程,且成绩至少高于选修编号为3-245的同学的cno,sno,degree,成绩由高到低排序

mysql> select cno,sno,degree from score where cno ='3-105' and degree >any(select degree from score where cno = '3-245') order by degree desc;
+-------+-----+--------+
| cno   | sno | degree |
+-------+-----+--------+
| 3-105 | 103 |     92 |
| 3-105 | 105 |     88 |
| 3-105 | 109 |     76 |
+-------+-----+--------+
3 rows in set (0.00 sec)

 max()

查询score表中最高分的学生学号和课程号

mysql> select sno,cno,degree from score where degree = (select max(degree) from score);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
+-----+-------+--------+
1 row in set (0.00 sec)

min()

查询score表中最低分的学生学号和课程号

mysql> select sno,cno,degree from score where degree = (select min(degree) from score);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 109 | 3-245 |     68 |
+-----+-------+--------+
1 row in set (0.00 sec)

avg()

查询每门课程的平均成绩

mysql> select avg(degree) from score group by cno;
+-------------+
| avg(degree) |
+-------------+
|     85.3333 |
|     76.3333 |
|     81.0000 |
+-------------+
3 rows in set (0.00 sec)

取别名

查询所有教师和同学的name,sex,birthday

mysql> select sname as name,ssex as sex,birthday as birthday1 from students-> union-> select tname,tsex,tbirthday from teacher;
+--------+-----+---------------------+
| name   | sex | birthday1           |
+--------+-----+---------------------+
| 张三   | 男  | 1977-09-01 00:00:00 |
| 李四   | 男  | 1975-10-02 00:00:00 |
| 王五   | 女  | 1976-01-23 00:00:00 |
| 铁柱   | 男  | 1976-02-20 00:00:00 |
| 小丽   | 女  | 1975-02-10 00:00:00 |
| 小红   | 男  | 1974-06-03 00:00:00 |
| 小绿   | 男  | 1976-02-20 00:00:00 |
| 大妞   | 女  | 1975-02-10 00:00:00 |
| 乖乖   | 男  | 1974-06-03 00:00:00 |
| 李想   | 男  | 1958-06-03 00:00:00 |
| 王超   | 女  | 1970-02-10 00:00:00 |
| 刘能   | 女  | 1961-06-03 00:00:00 |
| 张丹   | 男  | 1969-02-20 00:00:00 |
+--------+-----+---------------------+
13 rows in set (0.03 sec)

union和not in()

查询计算机系与电子工程系不同职称的教师的tname,prof

mysql> select tname,prof from teacher where tdepar='电子工程系' and prof not in(select prof from teacher where tdepar='计算机系')-> union-> select tname,prof from teacher where tdepar='计算机系' and prof not in(select prof from teacher where tdepar='电子工程系');
+--------+-----------+
| tname  | prof      |
+--------+-----------+
| 张丹   | 讲师      |
| 李想   | 副教授    |
+--------+-----------+
2 rows in set (0.00 sec)

参考

感谢code 158编程俱乐部

06 MySQL数据库--查询语句学习笔记相关推荐

  1. MySQL数据库——查询语句SELECT的学习总结,2W字超详细总结,值得收藏

    文章目录 一.基本的SELECT语句 1.SQL分类 2.SQL的语言规范 2.1.基本规则(必须遵守) 2.2.SQL大小写规范(建议遵守) 2.3.注释 2.4.数据导入指令 3.基本的SELEC ...

  2. 【面试必背】 常问的15个MySQL数据库查询语句,

    一.什么是数据库? 数据库是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文件中,但是在文件中读写 ...

  3. mysql数据库查询语句过程_mysql(一) SQL查询语句执行过程

    mysql基础架构 示意图 首先 mysql  大概分为 server层 和 存储引擎层 两个部分, 引擎的架构模式是插件形式的,mysql支持多种引擎如 InnoDB.MyISAM.Memory 等 ...

  4. mysql数据库查询语句_mysql数据库查询语句

    一   数据库的语句 1     关于数据库 创建数据库         create database 数据库名字(自己命名) 查看mysql下的所有数据库               show d ...

  5. MySQL数据库查询语句汇总

    基础查询 select [字段] from 表名 1,select * from emp;  #  查询所有数据 在数据量很庞大的情况下, 效率低, 不推荐使用 2,select empno,enam ...

  6. Day_03——MySQL数据库查询语句练习

    数据库查询操作作业 作业1 需求1:创建管家婆数据库 CREATE DATABASE gjp; 需求2:在数据库中创建两张表 表1:分类数据表,表名 gjp_sort. 字段包含:sid INT PR ...

  7. 数据库SQL语句学习笔记(6)-使用函数处理数据

    1.SQL也可以用函数来处理数据,函数一般是在数据上执行的,为数据的转换和处理提供了方便.但是每一个数据库管理系统(DBMS)都有特定的函数,事实上,只有少数几个函数被所有的DBMS等同地支持.例如, ...

  8. c# mysql数据库查询语句_C# mysql 查询

    展开全部 |mysql> select * from test_book1; +------+-----------+--------------------------------+ |e68 ...

  9. c#连接mysql数据库查询语句_C#连接MySQL数据库(增删改查)

    using System; using MySql.Data.MySqlClient; namespace CSharp直接连接MySQL { class Program { static void ...

最新文章

  1. 获取保存在沙盒中plist文件的用户的字典信息
  2. Linux下软件安装方法汇总
  3. 后台开发经典书籍--Linux多线程服务端编程:使用muduo C++网络库
  4. Java中集合(三)Stack
  5. Lead saved query bug
  6. 回调 that.setdata 数据不更新_重大利空落地,或损上亿利润,乐普医疗回调近四成...
  7. linux中dhcp如何配置两个子网,linux – 配置DHCP服务器以在同一VLAN上为多个子网提供服务...
  8. 小程序读取云服务器,小程序读取云服务器
  9. Google 中国招聘啦
  10. A+B Problem(再升级)
  11. Java并发容器,底层原理深入分析
  12. mysql concat函数进行模糊查询
  13. linux系统中jdk的卸载及安装
  14. LQR控制基本原理(包括Riccati方程具体推导过程)
  15. idea 远程调试resin
  16. 节奏旋律音乐制作软件-Native Instruments Maschine 2 v2.14.1 WiN
  17. ABAP-SAP 账号批量创建分配权限程序
  18. 斯坦福的《机器学习》课程上线了
  19. 兔子与兔子 字符串hash
  20. Codeforces 938C

热门文章

  1. 虚拟机下安装mysql(按相应步骤操作 / 图文)
  2. 【中科院】分子生物学-朱玉贤第四版-笔记-第7-8讲 翻译
  3. 支持nfc的android手机型号,哪些手机型号支持刷公交卡 支持nfc功能的手机有哪些汇总...
  4. java 计算包含中文字符串的真实长度
  5. DOS中del和rd的区别
  6. SWUST OJ 971: 统计利用先序遍历创建的二叉树的深度
  7. 计算机编程逻辑图,【图片】【附C++编程演示】计算机思维生成之逻辑编程篇【人工智能吧】_百度贴吧...
  8. git是什么,git怎么用?
  9. 环信IM SDK使用(四):环信会话列表实现及相关接口介绍
  10. 《深入浅出4G网络 LTE_EPC》阅读笔记一