数据库where条件查询、比较运算符、逻辑运算符、模糊查询、范围查询、空判断查询、order by排序、分页查询使用

数据库where条件查询*

where语句支持的运算符:
1.比较运算符
2. 逻辑运算符
3. 模糊查询
4. 范围查询
5. 空判断

select * from 表名 where 条件;
例:
select * from students where id = 1;

比较运算符查询
1. 等于: =
2. 大于: >
3. 大于等于: >=
4. 小于: <
5. 小于等于: <=
6. 不等于: != 或 <>

查询姓名不是“黄蓉”的学生:
select * from students where name != ‘⻩蓉’;
或者
select *from students where name <> ‘⻩蓉’;

逻辑运算符查询
1. and
2. or
3. not

查询年龄不在10岁到15岁之间的学生:
select * from students where not (age >= 10 and age <= 15);

多个条件判断想要作为一个整体,可以结合‘()’。
模糊查询
1. like是模糊查询关键字
2. %表示任意多个任意字符
3. _表示一个任意字符

查询姓黄的学生:
select * from students where name like ‘⻩%’;
查询姓黄并且“名”是一个字的学生:
select * from students where name like ‘⻩_’;
查询姓黄或叫靖的学生:
select * from students where name like ‘⻩%’ or name like’%靖’

范围查询

  1. between … and … 表示在一个连续的范围内查询
  2. in 表示在一个非连续的范围内查询

查询编号为3至8的学生:
select * from students where id between 3 and 8;
查询编号不是3至8的男生:
select* from students where (not id between 3 and 8) and gender=‘男’;

空判断查询

  1. 判断为空使用: is null
  2. 判断非空使用: is not null

查询没有填写身高的学生
select * from students where height is null;

注意: 1. 不能使用 where height = null 判断为空
2. 不能使用 where height != null 判断非空
3. null 不等于 ‘’ 空字符串

排序 order by
排序查询语法

select * from 表名 order by 列1 asc|desc [,列2 asc|desc,…]

	1. 先按照列1进行排序,如果列1的值相同时,则按照 列2 排序,以此类推 
	2. asc从小到大排列,即升序 
	3. desc从大到小排序,即降序 
	4. 默认按照列值从小到大排列(即asc关键字)

查询未删除男生信息,按学号降序:

select * from students where gender=1 and is_delete=0 order by id
desc;
显示所有的学生信息,先按照年龄从大–>小排序,
当年龄相同时 按照身高从高–>矮排序:
select * fromstudents order by age desc,height desc;

分页查询 limit

  • 应用在数据量大,一次性展示不完,一次给用户展示一页
  • 使用 limit 关键字可以限制数据显示数量,通过 limit 关键可以完成分页查询
  • limit 关键字后面的第一个参数是开始行索引(默认是0,不写就是0),第二个参数是查询条数

select * from 表名 limit start,count

  1. limit是分页查询关键字
  2. start表示开始行索引,默认是0
  3. count表示查询条数

查询年龄为1的同学前三个人

select * from students where gender=1 limit 0,3; 简写 select * from
students where gender=1 limit 3;

分页查询案例:
已知每页显示m条数据,求第n页显示的数据
提示: 关键是求每页的开始行索引
查询学生表,获取第n页数据的SQL语句:

select * from students limit (n-1)*m,m

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值