一,查询过程:
当mysql客户端向服务端发情查询请求以及服务器响应客户端的查询请求过程当中,数据通过mysql协议经过了一些列的转变,首先Mysql服务端通过mysql协议收到客户端请求,服务器首先要在自己的缓存查看是否有请求结果,有就直接返回,缓存用于缓存系统资源开销比较大的语句,如果缓存没有就将请求交给SQL解析器,解析器负责对语句此法分析,并使用预处理器生成查询列表,然后查询优化器基于执行开销最小即执行所消耗的资源选择消耗最小的线路,并生成查询执行计划,将查询计划交予查询执行引擎,查询执行引擎是将查询优化器有优化好的查询语句转换成对应表的存储引擎的API调用,后面的数据获取有对应的存储引擎根据查询执行引擎的API调用获取对方所需要的数据,然后将数据返回给查询执行引擎,数据结果在返还给用户,另外数据在返还给用户过程中会进行查询是否要将结果缓存,用户可以明确定义要缓存或不缓存的查询语句。
大概示意图如下:
二:DML常用命令:
DML(date manipulation language)语句:数据操纵语言,用户添加、删除、更新和查询数据库记录,并检查数据完整性,常用语句insert、delete、update和select等
MySQL查询分类:
单表查询:简单查询
多表查询:连续查询
联合查询:
选择和投影:
投影:挑选要显示的字段
SELECT 字段1 字段2,... FROM tb_name;
SELECT * FROM tb_name;
选择:调训符合条件的行
SELECT 字段1,。。。 FROM tb_name WHERE 子句;
布尔表达式
布尔条件表达式操作符:
= 等于
<=> 跟空值比较的安全方式,不会产生以外
<> 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
IS NULL 为空则予以显示
IS NOT NOLL 不为空为真,予以显示
LIKE;做模糊查询,通配符,
LIKE与RLIKE是做字符做比较,不能用于数值比较,数值要使用>等布尔表达式
%任意长度的任意字符
_:任意单个字符
RLIKE,REGEXP; 支持使用正则表达式,做字符串比较,数值比较只能使用大于小于等于
可以支持行首行尾等正则表达式
IN;判断指定字段的值是否在给定的列表中
BETWEEN;在X和X之间
X BETWEEN 10 AND 40:X在10和40之间
创建并使用一个数据库:
mysql> CREATE DATABASE ceshi;
mysql> use ceshi;
创建表:
mysql> CREATE TABLE st (SID INT UNSIGNED AUTO_INCREMENT NOT NULL UNIQUE KEY, Name CHAR(30) NOT NULL, Age TINYINT UNSIGNED NOT NULL, Gender ENUM ('F','M') NOT NULL, Tutor CHAR(30));
为表插入数据:
INSERT INTO st VALUES(1,'xiao zhang',27,'F','laozhang'),(2,'xiaoli',25,'M','laoli'),(3,'xiaowang',29,'M','laowang');
mysql> INSERT INTO st VALUES(4,'xiao song',46,'F','laosong'),(5,'xiaoxia',45,'M','laoxia'),(6,'xiaosun',44,'M','laosun');
查找年龄在40到45之间:关键字-用BETWEEN
mysql> SELECT Name,Age FROM st WHERE Age BETWEEN 40 AND 45;
+---------+-----+
| Name | Age |
+---------+-----+
| xiaoxia | 45 |
| xiaosun | 44 |
+---------+-----+
查找年龄正好25和29:关键字--用IN
IN用于指定一个匹配的列表,数字不用加引号,字符要加引号;
mysql> SELECT Name,Age FROM st WHERE Age IN(25,29);
+----------+-----+
| Name | Age |
+----------+-----+
| xiaoli | 25 |
| xiaowang | 29 |
+----------+-----+
mysql> SELECT Name,Age FROM st WHERE Name IN('xiaoxia','xiaowang');
+----------+-----+
| Name | Age |
+----------+-----+
| xiaowang | 29 |
| xiaoxia | 45 |
+----------+-----+
使用%和_通配符:
mysql> SELECT Name,Age FROM st WHERE Name LIKE ('x%');
+------------+-----+
| Name | Age |
+------------+-----+
| xiao zhang | 27 |
| xiaoli | 25 |
| xiaowang | 29 |
| xiao song | 46 |
| xiaoxia | 45 |
| xiaosun | 44 |
+------------+-----+
mysql> SELECT Name,Age FROM st WHERE Name LIKE ('_iaowang');
+----------+-----+
| Name | Age |
+----------+-----+
| xiaowang | 29 |
+----------+-----+
查询指定的非空和空字段:
关键字:使用NOT NULL和NULL
组合条件查找:
NOT 等于!
AND 等于&&
OR 等于||
mysql> select Name,Age,Gender FROM st WHERE Age > 30 AND GENDER = 'M';
+---------+-----+--------+
| Name | Age | Gender |
+---------+-----+--------+
| xiaoxia | 45 | M |
| xiaosun | 44 | M |
+---------+-----+--------+
排序:指定按照特指的字符串进行降序或升序排序
ASC,升序
mysql> SELECT Name,Age,Gender FROM st WHERE Age > 25 AND Gender = 'M' ORDER BY AGE ASC;
+----------+-----+--------+
| Name | Age | Gender |
+----------+-----+--------+
| xiaowang | 29 | M |
| xiaosun | 44 | M |
| xiaoxia | 45 | M |
+----------+-----+--------+
DESC 降序
mysql> SELECT Name,Age,Gender FROM st WHERE Age > 25 AND Gender = 'M' ORDER BY AGE DESC;
+----------+-----+--------+
| Name | Age | Gender |
+----------+-----+--------+
| xiaoxia | 45 | M |
| xiaosun | 44 | M |
| xiaowang | 29 | M |
+----------+-----+--------+
MySQL内置聚合函数:
SUM() 求和
AVG() 平均值
mysql> SELECT AVG(Age) FROM st;
+----------+
| AVG(Age) |
+----------+
| 34.1429 |
+----------+
MAX() 最大值
MIN() 最小值
COUNT() 统计符合条件的行:按照性别分组并统计每组的数值:
mysql> SELECT Gender,SUM(Age) FROM st GROUP BY Gender;
+--------+----------+
| Gender | SUM(Age) |
+--------+----------+
| | 0 |
| F | 141 |
| M | 143 |
+--------+----------+
按照性别分组并统计每组的数值:
mysql> SELECT Gender,SUM(Age) FROM st GROUP BY Gender;
+--------+----------+
| Gender | SUM(Age) |
+--------+----------+
| | 0 |
| F | 141 |
| M | 143 |
+--------+----------+
插入一个班级并给给学生设置班级号:
mysql> ALTER TABLE st ADD ClassID TINYINT UNSIGNED;
mysql> UPDATE st SET ClassID = 2 WHERE SID =1;
mysql> UPDATE st SET ClassID = 1 WHERE SID =2;
mysql> UPDATE st SET ClassID = 2 WHERE SID =3;
mysql> UPDATE st SET ClassID = 3 WHERE SID =4;
mysql> UPDATE st SET ClassID = 5 WHERE SID =5;
mysql> UPDATE st SET ClassID = 3 WHERE SID =6;
mysql> UPDATE st SET ClassID = 3 WHERE SID =7;
mysql> SELECT * FROM st;
+-----+------------+-----+--------+----------+---------+
| SID | Name | Age | Gender | Tutor | ClassID |
+-----+------------+-----+--------+----------+---------+
| 1 | xiao zhang | 27 | F | laozhang | 2 |
| 2 | xiaoli | 25 | M | laoli | 1 |
| 3 | xiaowang | 29 | M | laowang | 2 |
| 4 | xiao song | 46 | F | laosong | 3 |
| 5 | xiaoxia | 45 | M | laoxia | 5 |
| 6 | xiaosun | 44 | M | laosun | 3 |
| 7 | xiaozhao | 23 | F | NULL | 3 |
+-----+------------+-----+--------+----------+---------+
针对特定的关键字进行分组:GROUP BY
mysql> SELECT Name,SUM(Age),Gender FROM st GROUP BY Gender;
+------------+----------+--------+
| Name | SUM(Age) | Gender |
+------------+----------+--------+
| xiao zhang | 96 | F |
| xiaoli | 143 | M |
+------------+----------+--------+
过滤出一个班人数大于等于指定数量并显示姓名、年龄和数量:
关键字:使用HAVING对GROUP BY的结果做匹配:
mysql> SELECT CLassID,Count(Name),Age,Name FROM st GROUP BY ClassID HAVING Count(Name) >= 2;
+---------+-------------+-----+------------+
| CLassID | Count(Name) | Age | Name |
+---------+-------------+-----+------------+
| 2 | 3 | 27 | xiao zhang |
| 3 | 2 | 46 | xiao song |
+---------+-------------+-----+------------+
限定显示多少行:
#只是用一个数字表示显示1-N行,写X-N表示删除1-X行,显示X后的N行,如下:
mysql> select * from st LIMIT 3,3;
+-----+-----------+-----+--------+---------+---------+
| SID | Name | Age | Gender | Tutor | ClassID |
+-----+-----------+-----+--------+---------+---------+
| 4 | xiao song | 46 | F | laosong | 3 |
| 5 | xiaoxia | 45 | M | laoxia | 5 |
| 6 | xiaosun | 44 | M | laosun | 3 |
+-----+-----------+-----+--------+---------+---------+
显示所有的年龄,并且每个年龄只显示一次,即不显示重复的年龄:
关键字:使用DISTINCT-->distinct
练习:
1、以ClassID分组,显示每个班的人数;
2、以Gender分组,显示年龄之和;
3、以ClassID分组,显示平均年龄大于25的班级;
4、以Gender分组,显示各组中年龄大于25的学员的年龄之和,并分别计算个性别之和;
总结:
1、常用的查询组合:
SELECT ... FROM ... ORDER BY...;
SELECT ... FROM ... GROUP BY ... HAVING ...;
SELECT ... FROM ... WHERE ...;
SELECT ... ...;
SELECT ... FROM ... WHERE ... GROUP BY ... LIMIT... ;
SELECT DISTINCT ... FROM ... WHERE ... ORDER BY ...;
2、SELECT 查询语句执行过程:
FROM--->WHERE--->GROUP BY--->HAVING--->ORDER BY--->SELECT--->LIMIT
先执行FROM语句,去查找要执行命令的表,然后执行WHER语句,挑选出符合条件的行,如果有分组会数据,分组后使用HAVING进行条件过滤,随后使用ORDER BY对数据进行排序,然后才执行SELECT命令,最后如果有限制行则执行LIMIT,
本文出自 “Linux” 博客,转载请与作者联系!