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. LOJ #2135. 「ZJOI2015」幻想乡战略游戏

    #2135. 「ZJOI2015」幻想乡战略游戏 链接 分析: 动态点分治,求加权重心,带修改. 考虑如果知道了一个点s,如何求答案,那么首先可以点分治的思想,求每个联通块内所有点到分治中心距离和,然 ...

  2. C++学习之从C到C++

    头文件的包含 包含头文件可以不加.h结尾,如iostream,一些常用的头文件在引用时可以不加.h后缀,并在开头增加c,如: #include <cstdio> #include < ...

  3. VS相关

    快速显示函数名称 ctrl+alt+T 显示函数参数说明 ctrl+shift+space,光标放在函数里面 link 1123错误,vs2010的问题. 点击工程-属性-ManifestTool-I ...

  4. Flask学习-前言

    前言 使用Flask断断续续加起来快一年了,但是一直没有从源码层去了解其实现原理.加上自己python基础不扎实,所以准备从看一个开源项目开始,希望能够从中窥得武功精髓,让自己水平更上一层楼. Fla ...

  5. resource fork, Finder information, or similar detritus not allowed

    1.关闭当前项目和Xcode 2.打开终端或者iterm cd ~/Library/Developer/Xcode/DerivedData/ 3. xattr -rc . 4.重新打开项目 5.如果不 ...

  6. jenkins pipeline 部署

    一.git 版本控制结合jenkins 发布 sh-4.2$ git branch sh-4.2$ git chekout master sh-4.2$ git tag v1.1 sh-4.2$ gi ...

  7. Seay源代码审计系统的配置和安装

    2014年7月31日 Seay源代码审计系统2.1 时隔刚好一年之久,源代码审计系统再次更新,这次主要优化审计体验,优化了漏洞规则,算是小幅更新,原来使用者打开程序会提示自动更新. 1.优化原有规则, ...

  8. M1阶段事后总结

    设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述?我们组要爬取网上的内容供下一组使用,定义的不太清楚,因为用户只有下一个团队所以没有进行详细的需求分析 ...

  9. 【Alpha】功能规格说明书

    更新说明:从用户需求分析中剥离有关用户场景分析部分,加入功能规格说明书. Github地址:https://github.com/buaase/Phylab-Web/blob/master/docs/ ...

  10. VS2013安装及测试

    一.Visual Studio的安装 首先是Visual Studio英文版的安装,安装完成后,为了用的时候方便,我从官网下载Visual Studio 2013的语言包并安装. 二.进行单元测试. ...