python学习笔记-数据库04_where
使用where条件查询可以对表中的
数据进行筛选
,条件成立的记录会出现在结果集中。
where 条件查询语法格式如下 :
select * from 表名 wherer 条件;
例如:
select * from students where id = 1;
比较运算符查询
1. 等于: =
2. 大于: >
3. 大于等于: >=
4.小于:<
5.小于等于:<=
6.不等于:!= 或者 <>
逻辑运算符查询
and
or
not
比如:
// 查询编号大于3的女同学
select * from students where id > 3 and gender = 0;
// 查询编号小于4 或 没被删除的学生
select * from student where id < 4 or is_delete = 0;
// 查询年龄不在10岁到15岁之间的学生;
select * from students not (age >= 10 and age <= 15);
备注:
多个条件判断要作为一个整体,可以结合小括号()
模糊查询
like
是模糊查询
关键字
%
表示任意多个任意字符
_
表示一个任意字符
// 查询姓黄的学生:
select * from students where name like '黄%';
// 查询姓黄并且"名"是一个字的学生
select * from students where name like '黄_';
//查询姓黄或叫靖的学生
select * from students where name like ’黄%‘ or name like ’%靖‘;
范围查询
between .. and ..
表示在一个连续范围内
查询
in
表示在一个非连续
的范围内查询// 查询编号为3至8的学生
备注: 是包括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 = '男';
// 查询编号是3,5,7的学生
select * from students where id in (3,5,7);
// 查询编号不是3,5,7的学生
select * from students where id not in (3,5,7);
空判断查询
判断
为空
使用:is null
判断非空
使用:is not null
// 查询没有填写身高的学生;
select * from student where height is null;
备注:
不能使用 where height = null 判读为空
不能使用where height != null 判断非空
null 不等于"" 空字符串