MySQL简单查询详解-单表查询

                                        作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.查询的执行路径
  一条SQL查询语句的执行过程大致如下图所示:
  1>.客户端和服务端通过mysql协议进行通信,mysql服务器通过某种客户端发送来的查询语句的时候,首先会去本地检验缓存是否命中,如果请求的SQL语句之前有本查询过,就会把之前的查询结果直接返回给用户(查询缓存的功能最少要满足三成以上才有意义,如果所有的查询都去用缓存且所有的SQL没有一次被命中的话就毫无意义啦。);
  2>.如果查询的SQL没有命中,就会将SQL的请求交给解析器进行解析操作。必要时在预处理器的配合下,解析器会生成一个解析树,这个解析树会有很多条执行路径去查询我们想要得到的SQL结果;
  3>.如此多的执行路径就会交给查询优化器进行查询操作,查询优化器会选择资源开销最少的路径去执行SQL语句,与此同时,它还可能改写SQL语句,只要改写的结果和用户要查询的结果一致即可,查询优化器选择最优的一条执行路径之后又将这个结果交给查询执行计划进行排队(注意,MySQL的用户同时查询可能不止一个,因此并发的时候需要给它一个队列,哪些查询比较先进的会优先被查询到返回给用户);
  4>.以上的所有操作都没有执行的权限,最终执行查询操作的还是查询执行引擎。但是查询引擎并不能直接到磁盘上取数据,查询引擎本事只是把查询请求转换成对应表的存储引擎的API调用;
  5>.我们知道存储引擎其实是表类型,存储引擎根据查询执行引擎的API调用从磁盘上获取对方所需要的数据并层层返回给用户,在返回的途中,mysql还要考虑是否将查询结果进行缓存操作。

二.选择和投影
1.投影和选择
  投影其实就是挑选要符合的字段,选择就是挑选符合条件的行。
  投影:select 字段1,字段2,... from tb_name;
    常用语法格式:selcet * from tb_name;
  选择:select 字段1,字段2,.... from tb_name where 子句(布尔条件表达式);
 
2.布尔条件表达式操作符
  = 等值比较
  <=>:跟空值比较不会产生额外信息
  <>:不等值
  <:
  <=
  >
  >=
  IS NULL:是否为空
  IS NOT NULL:是否不空
  LIKE:支持的通配符%(任意长度的任意字符) _(任意单个字符)
  RLIKE,REGEXP:支持使用正则表达式作为条件(注意LIKE和RLIKE都是用来做字符比较的,如果用来做数值比较的话性能就相当低了。)
  IN:判断某行的某一字段的值是否在给定的列表中
  BETWEEN...AND....:判断指定的值是否位于指定的范围之间(比如,判断一个数值在10和20之间,我们就可以这样写"X BETWEEN 10 AND 20")
案例展示:
 mysql> create table stars(SID int unsigned auto_increment not null unique key,Name char() not null,Age tinyint unsigned not null,Gender Enum('boy','girl') not null, Tearch char());
Query OK, rows affected (0.01 sec) mysql>
mysql> desc stars;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| SID | int() unsigned | NO | PRI | NULL | auto_increment |
| Name | char() | NO | | NULL | |
| Age | tinyint() unsigned | NO | | NULL | |
| Gender | enum('boy','girl') | NO | | NULL | |
| Tearch | char() | YES | | NULL | |
+--------+---------------------+------+-----+---------+----------------+
rows in set (0.00 sec) mysql>
mysql> insert into stars values (,"sun wukong",,'boy','putilaozu'),(,'yinzhengjie',,'boy','himslef'),(,'liufei',,'boy','jia baoyu');
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql>
mysql> insert into stars values (,"han senyu",,'boy','cang laoshi'),(,'jia shanpeng',,'boy','ji laosi'),(,'wu zhiguang',,'boy','ma laoshi');
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec)
mysql> select * from stars where Age between and ;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | yinzhengjie | | boy | himslef |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql> select Name,Age from stars where Age in (,);
+-------------+-----+
| Name | Age |
+-------------+-----+
| yinzhengjie | |
| liufei | |
| wu zhiguang | |
+-------------+-----+
rows in set (0.00 sec) mysql>
mysql> select Name,Age from stars where Name rlike '^y.*';
+-------------+-----+
| Name | Age |
+-------------+-----+
| yinzhengjie | |
+-------------+-----+
row in set (0.00 sec) mysql>
mysql> insert into stars values(,'jenny',,'girl','Li ming'),(,'danny',,'boy',NULL);
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql> select Name,Tearch from stars where Tearch is NULL;
+-------+--------+
| Name | Tearch |
+-------+--------+
| danny | NULL |
+-------+--------+
row in set (0.00 sec) mysql> select Name,Tearch from stars where Tearch is NOT NULL;
+--------------+-------------+
| Name | Tearch |
+--------------+-------------+
| sun wukong | putilaozu |
| yinzhengjie | himslef |
| liufei | jia baoyu |
| han senyu | cang laoshi |
| jia shanpeng | ji laosi |
| wu zhiguang | ma laoshi |
| jenny | Li ming |
+--------------+-------------+
rows in set (0.00 sec) mysql>
3.组合条件测试
  NOT !
  AND &&
  OR ||
案例展示:
 mysql> select database();
+-------------+
| database() |
+-------------+
| yinzhengjie |
+-------------+
row in set (0.00 sec) mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| stars |
| t1 |
+-----------------------+
rows in set (0.01 sec) mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql> select Name,Age from stars where Age > and Gender = 'boy';
+-------------+-----+
| Name | Age |
+-------------+-----+
| sun wukong | |
| yinzhengjie | |
| liufei | |
| han senyu | |
| wu zhiguang | |
+-------------+-----+
rows in set (0.00 sec) mysql>
4.排序
  order by ‘排序字段’
  默认为升序:ASC
  降序:DESC
案例展示:
 mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql> select Name,Age from stars where Age > and Gender = 'boy' order by Name desc;
+-------------+-----+
| Name | Age |
+-------------+-----+
| yinzhengjie | |
| wu zhiguang | |
| sun wukong | |
| liufei | |
| han senyu | |
+-------------+-----+
rows in set (0.00 sec) mysql> select Name,Age from stars where Age > and Gender = 'boy' order by Name asc;
+-------------+-----+
| Name | Age |
+-------------+-----+
| han senyu | |
| liufei | |
| sun wukong | |
| wu zhiguang | |
| yinzhengjie | |
+-------------+-----+
rows in set (0.00 sec) mysql>
5.常用内置的聚合函数
  关键字如下:
    1>.SUM():运算和
    2>.AVG():运算平均值
    3>.MAX():运算最大值
    5>.MIN():运算最小值
    6>.COUNT():运算个数统计
案例展示:
 mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql> select SUM(Age) from stars;
+----------+
| SUM(Age) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql> select MAX(Age) from stars;
+----------+
| MAX(Age) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql> select MIN(Age) from stars;
+----------+
| MIN(Age) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql> select COUNT(Age) from stars;
+------------+
| COUNT(Age) |
+------------+
| |
+------------+
row in set (0.00 sec) mysql> select COUNT(*) from stars; #这种用法不建议,“*”的效率是非常低的,不如加入一个字段进去!
+----------+
| COUNT(*) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql>
mysql> select SUM(Age) from stars where Age > ;
+----------+
| SUM(Age) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql>
6.分组
  关键字:group by
案例展示:
 mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql>
mysql> select Gender,SUM(Age) from stars group by Gender;
+--------+----------+
| Gender | SUM(Age) |
+--------+----------+
| boy | |
| girl | |
+--------+----------+
rows in set (0.00 sec) mysql>
7.对分组的条件过滤
  关键字:having
案例展示:
 mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql> alter table stars add ClassID tinyint unsigned;
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+--------------+-----+--------+-------------+---------+
| | sun wukong | | boy | putilaozu | NULL |
| | yinzhengjie | | boy | himslef | NULL |
| | liufei | | boy | jia baoyu | NULL |
| | han senyu | | boy | cang laoshi | NULL |
| | jia shanpeng | | boy | ji laosi | NULL |
| | wu zhiguang | | boy | ma laoshi | NULL |
| | jenny | | girl | Li ming | NULL |
| | danny | | boy | NULL | NULL |
+-----+--------------+-----+--------+-------------+---------+
rows in set (0.00 sec) mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+--------------+-----+--------+-------------+---------+
| | sun wukong | | boy | putilaozu | |
| | yinzhengjie | | boy | himslef | |
| | liufei | | boy | jia baoyu | |
| | han senyu | | boy | cang laoshi | |
| | jia shanpeng | | boy | ji laosi | |
| | wu zhiguang | | boy | ma laoshi | |
| | jenny | | girl | Li ming | |
| | danny | | boy | NULL | |
+-----+--------------+-----+--------+-------------+---------+
rows in set (0.00 sec) mysql>
mysql> select ClassID,Count(Name),sum(Age) from stars group by ClassID;
+---------+-------------+----------+
| ClassID | Count(Name) | sum(Age) |
+---------+-------------+----------+
| | | |
| | | |
| | | |
| | | |
| | | |
+---------+-------------+----------+
rows in set (0.00 sec) mysql>
mysql> select ClassID,Count(Name) from stars group by ClassID having Count(Name) >=;
+---------+-------------+
| ClassID | Count(Name) |
+---------+-------------+
| | |
| | |
+---------+-------------+
rows in set (0.00 sec) mysql>
mysql> select ClassID from stars group by ClassID having sum(Age) >=;
+---------+
| ClassID |
+---------+
| |
+---------+
row in set (0.00 sec) mysql>
8.只返回有用的行
  关键字:LIMIT(一个数为显示的行数,两个数字为偏移第一个数字行,显示第二个数字)
案例展示:
 mysql> select * from stars ;
+-----+--------------+-----+--------+-------------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+--------------+-----+--------+-------------+---------+
| | sun wukong | | boy | putilaozu | |
| | yinzhengjie | | boy | himslef | |
| | liufei | | boy | jia baoyu | |
| | han senyu | | boy | cang laoshi | |
| | jia shanpeng | | boy | ji laosi | |
| | wu zhiguang | | boy | ma laoshi | |
| | jenny | | girl | Li ming | |
| | danny | | boy | NULL | |
+-----+--------------+-----+--------+-------------+---------+
rows in set (0.00 sec) mysql>
mysql> select * from stars limit ;
+-----+-------------+-----+--------+-----------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+-------------+-----+--------+-----------+---------+
| | sun wukong | | boy | putilaozu | |
| | yinzhengjie | | boy | himslef | |
+-----+-------------+-----+--------+-----------+---------+
rows in set (0.00 sec) mysql> select * from stars limit ,;
+-----+--------------+-----+--------+-------------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+--------------+-----+--------+-------------+---------+
| | liufei | | boy | jia baoyu | |
| | han senyu | | boy | cang laoshi | |
| | jia shanpeng | | boy | ji laosi | |
+-----+--------------+-----+--------+-------------+---------+
rows in set (0.00 sec) mysql>
9.select语句的执行流程
  from clause --> where clause --> group by --> having clause -->order by --> select -->limit
select常用的修饰符:
  distinct 重复的只显示一次
  SQL_CACHE 缓存查询结果
  SQL_NO_CACHE 不缓存查询结果
 
10.小试牛刀
  我比较喜欢看日本的一个动漫叫《火影忍者》,如果要存储一些人员信息的话,我们应该如何搞呢?请用多张表将下面表格的信息保存。相信这对大家来说应该都是小case,如果你忘记如何写SQL语句的话,可以参考:http://www.cnblogs.com/yinzhengjie/p/7862654.html

MySQL简单查询详解-单表查询的更多相关文章

  1. mysql查询操作之单表查询、多表查询、子查询

    一.单表查询 单表查询的完整语法: .完整语法(语法级别关键字的排列顺序如下) select distinct 字段1,字段2,字段3,... from 库名.表名 where 约束条件 group ...

  2. MySQL数据库实验二:单表查询

    实验二   单表查询 一.实验目的 理解SELECT语句的操作和基本使用方法. 二.实验环境 是MS SQL SERVER 2005的中文客户端. 三.实验示例 1.查询全体学生的姓名.学号.所在系. ...

  3. 不使用left-join等多表关联查询,只用单表查询和Java程序,简便实现“多表查询”效果

    上次我们提到,不使用left-loin关联查询,可能是为了提高效率或者配置缓存,也可以简化一下sql语句的编写.只写单表查询,sql真得太简单了.问题是,查询多个表的数据还是非常需要的. 因此,存在这 ...

  4. Hibernate第十篇【Hibernate查询详解、分页查询】

    前言 在Hibernate的第二篇中只是简单地说了Hibernate的几种查询方式-.到目前为止,我们都是使用一些简单的主键查询阿-使用HQL查询所有的数据-.本博文主要讲解Hibernate的查询操 ...

  5. MySQL常用查询命令(单表查询)

    查询语法如下: select... from... where... group by... (having)... order by...; 顺序是from (从指定表中) where (具体条件) ...

  6. MySql语句常用命令整理---单表查询

    初始化t_employee表 创建t_employee表 -- DROP TABLE IF EXISTS test; CREATE TABLE t_employee ( _id INTEGER PRI ...

  7. MySQL(九)之数据表的查询详解(SELECT语法)一

    这一篇是MySQL中的重点也是相对于MySQL中比较难得地方,个人觉得要好好的去归类,并多去练一下题目.MySQL的查询也是在笔试中必有的题目.希望我的这篇博客能帮助到大家! 重感冒下的我,很难受!k ...

  8. mysql数据库之单表查询

    单标查询 单表查询语句 关键字执行的优先级 简单查询 where约束 group by 聚合函数 HAVING过滤 order by 查询排序 LIMIT限制查询的记录数 使用正则表达式查询 单表查询 ...

  9. MySql(六)单表查询

    十.单表查询 一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制 ...

随机推荐

  1. JAVA eclipse Maven项目红叹号解决方案

    我是通过 Windows --> show view --> problems 查看到发现 ch.qos.logback 1.1.1 出现了错误, 于是我换成了 ch.qos.logbac ...

  2. C#易忘点

    下面是自己总结的一些C#语言方面用的少容易忘的地方,总结一下,在用的时候看一下.(仅针对本人) 参数数组 定义一个函数,用来取得数字的和,但是数字的个数不确定. 解决方案: 1,定义一个函数,参数传递 ...

  3. 关于OBS获取显示器黑屏的解决办法

    近来看到许多人说OBS获取显示器源的时候黑屏,下面介绍下相关处理办法. 第一种,先尝试把OBS程序的兼容性设置成Win 7和管理员身份,具体操作: 设置成这样,如果能够获取到显示器,那么问题解决,否则 ...

  4. mui框架(一)

    1.界面初始化 初始化就是把一切程序设为默认状态,把没准备的准备好. mui框架将很多功能配置都集中在mui.init方法中,要使用某项功能,只需要在mui.init方法中完成对应参数配置即可,目前支 ...

  5. 20135202闫佳歆--week7 可执行程序的装载--学习笔记

    此为个人学习笔记存档 week 7 可执行程序的装载 一.预处理.编译.链接和目标文件的格式 可执行文件的创建--预处理.编译和链接 cd Code vi hello.c gcc -E -o hell ...

  6. 2017-2018 第一学期201623班《程序设计与数据结构》-第5&6周作业问题总结

    一.作业内容 第5周作业 http://www.cnblogs.com/rocedu/p/7484252.html#WEEK05 第6周作业 http://www.cnblogs.com/rocedu ...

  7. 第二个Sprint冲刺第四天(燃尽图)

  8. What is the difference between WinRT, UWP and WPF?

    在学习UWP的过程中确实有这个迷惑,在此分享一下. UWP (Universal Windows platform), Metro and WinRT are all result of Micros ...

  9. idea不能跟随输入法问题

    在写注释的时候会发现输入法不跟随,这是idea工具本身存在的bug,这个问题很头疼,我找了好多办法都不行,比如删除idea自带的jre,这个办法对我的2018.1.5版本并不适用,以下办法是不需要删除 ...

  10. Java时间的转换

    String DATE_FORMAT = "yyyy-MM-dd"; LocalDate localDate = LocalDate.now(); long startTime = ...