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

时间:2023-11-12 00:04:20

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

                                        作者:尹正杰

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

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