- 很高兴您打开了本篇博客
- 主要作用:通过学习和复习,整理mysql笔记,为前端/后端/大数据项目铺设基石
- 一,数据库概叙,与MySql的环境图文安装步骤_nosql-CSDN博客
- 二,SQL数据类型_int sql格式-CSDN博客
- 三,MYSQL-数据库定义语言 DDL(Data Definition Language)-CSDN博客
- 四,MySql数据库之表关系与外键约束-CSDN博客
- 五,MySQL 数据库操作语言DML(Data Manipulation Language)-CSDN博客
- 六,MySQL之表关系_mysql表之间的关系-CSDN博客
- 本篇是系列专栏的 第七篇
- 主要内容:全部的SQL查询语
- 学习:知识的初次邂逅
- 复习:知识的温故知新
目录
单表:
一,基本查询
1,简单查询全部信息:
select * from 表名;
2,简单查询指定单条字段信息:
select 指定列名 from 表名;
3,简单查询多个字段名:
select 字段1,字段2,字段3,…… from 表名;
4,简单查询字段名带运算:
select 字段[+-*/]值 from 表名;
5,简单查询去重的字段名之指定单条字段:
select disnct 字段 from 表名;
6,简单查询去重的字段名之多条字段:
select disnct 字段1,字段2 from 表名;
--会对比全部字段 过滤重复值 只要2个字段任意一个不一样 都不会过滤 都会显示出来
7,简单查询值取别名:
select 字段 as 别名 from 表名 as 别名;
--省略别名的写法
select 字段 别名,字段2,别名2 from 表名 别名;
二,条件查询 where
1,条件查询之基本查询:
select * from 表名 where 条件;
2,条件查询之比较运算符查询:
select * from 表名 where 字段 [= | < | > | != | <> | <= | >=] 值;
3,条件查询之逻辑运算符查询:
select * from 表名 where 条件 [and | or] 条件;
select * from 表名 where 条件 Not 条件;
4,条件查询之连续范围查询:
select * from 表名 where 字段 between 值1 and 值2
between……and …… 是小于等于 到大于等于的区间;
等同于:
select * from 表名 where 字段 >= 值1 and 字段<=值2 ;
5,条件查询值非连续范围查询:
select * from 表名 where 字段 in(值1,值2,值3,……);
等同于:
select * from 表名 where 列名 = 值1 or 列名 = 值2 or 列名= 值3 or ……
6,条件查询之模糊查询:
select * from 表名 where 字段 like '% | _'
其中 %是多个字符,_是一个字符
比如查询用户表 姓王的有
select * from 表名 where name like '王%';
比如查询用户表 姓名 是三个字的 下面是 三个下划线 _ _ _
select * from 表名 where name like '___'
7,条件查询之非空查询:
select * from 表名 where 列名 is null;
select * from 表名 where 列名 is not null;
8,条件查询之正则表达式查询:
--在常规 比较运算符无法实现的时候 可以常识用正则匹配;
select * from 表名 where 字段 regexp '填入正则匹配的规则';
场景,查询电话字段里面时候都是符合国内电话号码格式的结果集:
select * from 表名 where phone REGEXP '^1[3|4|5|7|8][0-9]{9}$';
9,条件查询之判断子查询时候存在结果集:
select * from 表名 where is exists (子查询语句);
三,排序查询 order by [asc/desc]
1,正序,默认就是正序 asc
select * from 表名 order by 列名
2,倒序查询 需要加关键字 desc
select * from 表名 order by 列名 desc;
四,聚合函数查询
1,统计次数查询 count()
select count(列名) from 表名 where 条件;
2,平均查询 avg()
select avg(列名) from 表名;
3,最大值查询 sum()
select sum(列名) from 表名;
4,最小值查询 min()
select min(字段) from 表名;
5,最大值查询 max()
select max(字段) from 表名;
五,分页查询 limit m,n
select * from 表名 Limit m ,n;
其中n是每页显示的数据 数目;
m = (page -1)*n
比如 显示第一页 ,一页上面显示10条数据
select * from 表名 limit (1-1)*10,10;
六,分组查询 group by
分组必统计,分组不统计就是耍流氓!
1,分组基本查询
select 字段X from 表名 group by 字段X;
2,分组带聚合查询
select 字段X,聚合函数(字段X) from 表名 group by 字段X;
3,分组带聚合查询带条件查询
select 字段名,聚合函数(字段X) from 表名 where 条件 group by 字段名;
select 字段名,聚合函数(字段名) from 表名 where 条件 group by 字段名 having 条件;
4,多个分组查询
select 字段名 from 表名 group by 字段1,字段2;
七,子查询
1,子查询作为条件查询1:
select * from 表名 where 字段 = (select ……);
2,子查询作为条件查询2:
select * from 表名 where 字段 in (select ……)
3,子查询作为表格查询:
select * from 表名1,(select ……) as 表别名;
示例:
select * from (select * from (select * from user)as s2)as s1;
---隔着套娃呢
多表
--- 连接必加条件,连接不加条件就是耍流氓;
一,交叉链接查询
交叉连接就是不加条件的连接查询 会产生两个表的并集。也就是产生笛卡尔乘积。如果两个表数据比较大,那么造成的后果难以预料。
select * from 表1,表2;
select * from 表1 cross join 表2;
二,内连接查询
1,等值连接
1,等值内连接查询语法1
select * from 表名1 as 别名1,表名2 as 别名2 where 别名1.字段=别名2.字段;
2,等值内连接查询语法2
select * from 表名1
inner join 表名2
on 表名1.字段(外键或者主键字段)=表名.主键或者外键字段
3,等值内连接查询语法3
select *
from 表名1 join 表名2 on 表名1.字段 = 表名2.字段 where 条件;
2,非等值内连接查询语法
select * from 表名1 as t1,表名2 as t2 where 字段 between 值1 and 值2;
应用场景:查询薪资范围与岗位级别
SELECT salary, 岗位级别
FROM 员工部门详情表 e, 集团公司岗位级别表 g
WHERE e.salary BETWEEN g.值1 AND g.值2;
3,自然连接
select * from 表名1 natural join 表名2;
---自动查询2张表相等的字段
---ps 这玩意还是我们手动设置连接的好,免得查询结果集出现不可预料的情况;
三,外连接查询
1,左外连接查询
select *
from 表名1 left join 表名2
where 表名1.字段=表名2.字段;
2,右外连接查询
select *
from 表名1 right join 表名2
where 表名1.字段 = 表名2.字段;
3,全外连接查询
1,去重查询
select * from 表名1 left join 表名2 on 表名1.字段=表名2.字段
unionall
select * from 表名1 right join 表名2 on 表名1.字段=表名2.字段;
2,不去重查询
select * from 表名1 left join 表名2 on 表名1.字段=表名2.字段
union
slect * from 表名1 right join 表名2 on 表名1.字段=表名2.字段;
四,自连接查询
1,仅仅适用于自关联的表
关于自关联的表 :
2,自连接查询 可以采用以上任何连接的语法查询
select * from 本表 as t1,本表 as t2 where 条件;
----自连接仅适用于具有自关联关系的表;
----自关联查询适用于所有的内外连接查询;
总结 mysql的查询语句优先级
select 字段1,聚合函数(字段)
from 表名1
join (select ……子查询语句)
on 连接条件
where 条件
group by 分组字段
having 分组条件
order by 排序字段 [asc/desc]
limit m,n;
优先级
-
0,遇到子查询,先走子查询;
-
1,执行whrer条件 对原始表进行过滤
-
2,执行 group by 对原始表过滤后的数据进行分组
-
3,执行聚合函数 用于计算
-
4,执行having 进一步对数据过滤
-
5,order by 永远都是倒数第二个,有了数据才能排序
-
6,limit 永远都是最后一个 截取最终呈现的结果
- 学习:知识的初次邂逅
- 复习:知识的温故知新
- 谢谢观看~