在MySQL数据库中查询操作是最重要的一个操作
1、投影查询
* : 代表所有字段(列)
查询所有, 这个所有分成两个部分
(1)所有的行数据 (2)所有的列数据
select * from student;
这种情况会造成一种后果: 当我们数据量比较大,且数据表的字段比较多的时候, 效率很低;
真实情况,可能我们不需要这么多数据,比如: 只需要学生的 姓名和性别;
查询出来的这些数据是存放在什么地方的? =>内存 =>查询所有的字段是比较消耗内存;
这个时候为了提升效率 => 投影查询 =>指定字段查询;
select stu_name,stu_gender from student;#准确找到姓名和性别
一般来说真实情况下的查询都是投影查询。
2、限制查询(*)
关键字 : limit ;这个限制查询在数据库的查询是用来干啥的 => 用来分页;
为什么需要分页? =》 比如:京东 商品数据 上万条 => 一次性查询出来 =>直接内存溢出 => OOM =>宕机/卡,所以就需要将数据进行分批次的查询出来 => limit;
limit 有两种用法
(1)限制条数
=> limit n;n表示条数
select * from student limit 2;#只显示前两条信息
(2)分段
=>limit index,length;
index : 下标 从0开始 ; length : 长度
#第一页
select * from student limit 0,3;
#第二页
select * from student limit 3,3;
#第三页
select * from student limit 6,3;
代码中 limit 0,3;表示 查询的第一页信息的下标从0开始,长度为3,第二页和第三页类推
所以得以下结论
limit (page-1)*pageSize, pageSize;
3、条件查询
关键字: where => 在哪里? => 条件,通过where后面的条件对数据进行查询
(1)单条件
select * from sutdent where stu_name="JACK";#只有一个条件,只查询对应的数据
(2)多条件
在JS中 多条件用的逻辑运算符: &&(与) ||(或)!(非)
但是在数据库中多条件用的逻辑运算符为:and or not
select * from student where stu_name="ROSE" and stu_age >20;#表示查询stu_name="ROSE"和stu_age >20的数据,而且两个条件需要同时满足;
select * from student where stu_age > 21 or stu_gender="男";#查询stu_age > 21或者stu_gender="男"的数据,只满足其中一个条件即可;
not 有几个组合
以某个字段值是否为空作为条件: is null/ is not null
#查询数据中stu_edu是否为空的数据
select * from student where stu_edu is not null;
select * from student where stu_edu is null;
not 还有一种情况 : 判断某个值是否在某个集合中 in / not in
select * from student where stu_code in(3,5,7);#查询stu_code为3,5,7的数据
select * from student where stu_code not in(3,5,7);#查询除开stu_code是3,5,7的其他数据
(3)比较
> , >= , < , <= , <>(不等于) , != , between ...and..
between ...and.. 等价于 >= and <=
#示例
select * from student where stu_age <> 22;
select * from student where stu_age != 22;
select * from student where stu_age between 22 and 23;
(4)结合
将条件,限制,投影 结合起来 ; 限制查询一定是放在SQL语句的最后
select stu_name,stu_age from student where stu_age >22;
select stu_name,stu_age from student where stu_age >22 limit 1;
4、模糊查询
模糊查询,就是不确定这个条件,完整的值是什么,只知道一部分。 只要有搜索功能基本都会有模糊查询,但是模糊查询性能很差。
关键词:like => 像
(1)全模糊
=> 只要对应的字段值中包含 条件值 =>就满足条件
语法: select * from 表名 where 字段名 like "%条件值%";
select * from student where stu_name like '%k%';
(2)半模糊
①以什么开始 ;语法: like "条件值%";
select * from student where where stu_name 'k%';
②以什么结束;语法:like "%条件值";
select * from student where where stu_name '%k';
③占位符:_ 一个下划线代表一个字符, 这个字符可以是任意值
语法: like "_条件值";
select * from student where stu_name like "_c_";
模糊查询核心点是什么? => 正则表达式
利用正则来进行模糊查询 , 关键字: REGEXP
语法: select * from 表名 where 字段名 REGEXP '正则表达式'
这里面的正则表达式和JS部分一致
select * from student where stu_name REGEXP '';
5、排序查询
排序在实际的业务相当多,比如:销量排行, 热歌榜, 成绩同级排名,热点话题排名。。。。。。
关键字 : order by 语法:order by 字段名 asc/desc
asc 升序 => 默认值 desc 降序 =>用的多
(1)单字段排序
select * from student order by stu_age asc;#对stu_age进行升序排序
select * from student order by stu_age desc;#对stu_age进行降序排序
(2)多字段
如果一个字段排序确定不了,那么我们就需要额外的字段来进行排序 => 多字段排序
语法: order by 字段名1 asc/desc, 字段名2 asc/desc
select * from student order by stu_age desc, stu_code desc;#先对stu_age 进行降序排序,再对stu_code 进行降序排序
6、分组查询
语法:select * from 表名 group by 字段名
每个分组是一个虚拟表 所以分组之后只能看到虚拟表中的第一条数据