MYSQL study notes seven (underlying query)

A, MYSQL underlying query
1.1 SELECT complete syntax
select list of fields from the data source; ## field list can be . It means those queries all fields
select the option to re-list field [as a field alias] from a data source [where clause] [group by clause] [HAVING clause] [order by clause] [clause limit];

eg:

* The FROM testtablenew the SELECT
. 1
MYSQL study notes seven (underlying query)
1.2 deduplication option
to option refers to the weight of the query results to the same record weight:
All: not re
DISTINCT: weight to
the grammatical structure is as follows:

select [all | distinct] from table field list;
. 1
EG:
In Table testtablenew insert duplicate data (statement into data using six notes). Data was as follows:
MYSQL study notes seven (underlying query)
using data testtablenew sql statement in the query, the query results can not contain duplicate data, which query is as follows:

## a wording:
the SELECT DISTINCT * the FROM testtablenew
## written two:
the SELECT DISTINCT ID, the FullName, the FROM testtablenew Age
MYSQL study notes seven (underlying query)
Note: de-duplication is only for check out the record, rather than records stored in the table. If the query is only certain fields, then go for the weight of these fields.

1.3 字段别名
字段别名是给查询的字段再定义一个名字(改名字只在查询结果中应用)
字段别名一般都是辅助了解字段意义(比如我们定义的名字是name,我们希望返回给用户的结果显示成姓名)、简写字段名
具体语法如下:

select 字段 as 字段别名 from 表名;
select 字段 字段别名 from 表名;
1
2
eg:
查询testtablenew表中的数据,并对字段进行别名设置。使其显示出来的字段为编号、姓名、年龄

SELECT DISTINCT ID '编号', fullname AS '姓名' , age '年龄' FROM testtablenew
1
执行结果如下:
MYSQL study notes seven (underlying query)
1.4 数据源
数据源可以是单表数据源,多表数据源,其查询语法格式如下:

select 字段列表 from 表名;
select 字段列表 from 表名1,表名2,…; 【多表查询时是将每个表中的x条记录与另一个表y条记录组成结果,组成的结果的记录条数为x*y】【可以称为笛卡尔积】
select 字段列表 fromr (select语句) as 表别名;【这是将一个查询结果作为一个查询的目标二维表,需要将查询结果定义成一个表别名才能作为数据源】
1
2
3
1.5 MYSQL查询的子句
编号 子句 作用 说明
1 where子句 条件查询 按照“条件表达式”指定的条件进行查询。
2 group by子句 分组 按照“属性名”指定的字段进行分组。group by子句通常和count()、sum()等聚合函数一起使用
3 having子句 筛选 有group by才能having子句,只有满足“条件表达式”中指定的条件的才能够输出
4 order by子句 排序 按照“按照“属性名”指定的字段进行排序。排序方式由“asc”和“desc”两个参数指出,默认是按照“asc”来排序,即升序。
5 limit 限制结果集
1.5.1 where子句
where子句是主要是用来筛选符合条件的结果。
where常见的有种用法:

基于值:
= :
where 字段 =值 ;查找出对应字段等于对应值的记录。(相似的,<是小于对应值,<=是小于等于对应值,>是大于对应值,>=是大于等于对应值,!=是不等于)。

SELECT FROM testtablenew WHERE fullname='001'
SELECT
FROM testtablenew WHERE ID>5
SELECT * FROM testtablenew WHERE ID!=7
1
2
3
like:
where 字段 like 值 ;功能与 = 相似 ,但可以使用模糊匹配来查找结果。模糊查询的匹配字符常见的有四种:

编号 匹配符 说明
1 % 表示任意 0 个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示
2 _ 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句。
3 [] 表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
4 [^] 表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
SELECT FROM testtablenew WHERE fullname LIKE '%7'
SELECT
FROM testtablenew WHERE fullname LIKE '_01'
SELECT FROM testtablenew WHERE fullname REGEXP '00[1-2]'
SELECT
FROM testtablenew WHERE fullname REGEXP '00[^1-2]'
1
2
3
4
基于值的范围
in:
where 字段 in 范围;查找出对应字段的值在所指定范围的记录。

SELECT * FROM testtablenew WHERE ID IN (1,2,3,4)
1
not in :
where 字段 not in 范围;查找出对应字段的值不在所指定范围的记录。

SELECT * FROM testtablenew WHERE ID NOT IN (1,2,3,4)
1
between x and y :
where 字段 between x and y;查找出对应字段的值在闭区间[x,y]范围的记录。

SELECT * FROM testtablenew WHERE ID BETWEEN 3 AND 5
1
条件复合
or :
where 条件1 or 条件2… ; 查找出符合条件1或符合条件2的记录。

SELECT * FROM testtablenew WHERE ID =5 OR fullname='002'
1
and:
where 条件1 and 条件2… ; 查找出符合条件1并且符合条件2的记录。

SELECT * FROM testtablenew WHERE ID =5 AND age>16
1
not :
where not 条件1 ;查找出不符合条件的所有记录。

SELECT * FROM testtablenew WHERE NOT ID =5
1
&&的功能与and相同;||与or功能类似,!与not 功能类似。

1.5.2 group by子句
“Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理。

select类别, sum(数量) as数量之和
from A
groupby类别
1
2
3
注:group by语句中select指定的字段必须是“分组依据字段”,其他字段若想出现在select中则必须包含在聚合函数中。

mysql中五种常用的聚合函数:

编号 聚合函数 作用
1 max(列名) 求最大值
2 min(列名) 求最小值
3 sum(列名) 求和
4 avg(列名) 求平均值
5 count(列名) 统计记录的条数
求最大值
获取testtablenew 表中的最大ID

SELECT MAX(ID) FROM testtablenew
1
求最小值
获取testtablenew 表中的最小ID

SELECT MIN(ID) FROM testtablenew
1
求和
求testtablenew 表中所有ID之和。

SELECT SUM(ID) FROM testtablenew
1
求平均值
求testtablenew 表中ID的平均值。

SELECT AVG(ID) FROM testtablenew
1
统计记录的条数
求testtablenew 表中有多少条数据。

SELECT COUNT(ID) FROM testtablenew
SELECT COUNT(*) FROM testtablenew

eg:
统计testtablenew 表中的信息,分别计算各个年龄的数据条数。sql语句如下:

SELECT age,COUNT(*) FROM testtablenew
GROUP BY age

命令结果执行如下:
MYSQL study notes seven (underlying query)
1.5.3 having子句
having子句可以让我们筛选成组后的各种数据,where子句在聚合前先筛选记录,也就是说作用在group by和having子句前。而having子句在聚合后对组记录进行筛选。语法格式如下:

select类别, sum(数量) as数量之和from A
group by类别
having sum(数量) >18
1
2
3
eg:
在testtablenew表中,查询age条数大于等于4条的age。sql语句如下:

SELECT age,COUNT() FROM testtablenew
GROUP BY age
HAVING COUNT(
)>=4
1
2
3
Having和Where的联合使用,句法结构如下:

select类别, SUM(数量)from A
where数量>8
groupby类别
havingSUM(数量) >10
1
2
3
4
where和having的区别:

having 和where 都是用来筛选用的
having 是筛选组 而where是筛选记录
having一般跟在group by之后,执行记录组选择的一部分来工作的。where则是执行所有数据来工作的。
再者having可以用聚合函数,如having sum(qty)>1000
1.5.4 order by 子句
SELECT FROM testtablenew ORDER BY ID #按照ID进行升序(默认)
SELECT
FROM testtablenew ORDER BY ID DESC #按照ID进行降序
SELECT FROM testtablenew ORDER BY ID ASC #按照ID进行升序
SELECT
FROM testtablenew ORDER BY RAND() #随机排列,效率不高
1
2
3
4
1.5.5 limit 子句
语法结构:

limit [offset,] N
offset 偏移量,可选,不写则相当于limit 0,N
N 取出条目
1
2
3
eg:
取ID第1-3的数据
SELECT * FROM testtablenew ORDER BY ID ASC LIMIT 2,3;

1.5.6 summarized
No. clauses must indicate whether
1 select to return the column or is represented by Formula
2 form retrieved from table data using only data from the selected table
3 where the row level filtering No
4 group by grouping only described by when aggregation is calculated using the group
5 having no group-stage filter
6 order by sort order output no
number of rows to retrieve 7 limit NO

Guess you like

Origin blog.51cto.com/14525650/2436575