美文网首页
python学习笔记-数据库04_where

python学习笔记-数据库04_where

作者: MR_詹 | 来源:发表于2020-11-09 23:08 被阅读0次

使用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 不等于"" 空字符串

相关文章

网友评论

      本文标题:python学习笔记-数据库04_where

      本文链接:https://www.haomeiwen.com/subject/sackbktx.html