1、条件
使用where子句对表中的数据筛选,符号条件的数据会出现在结果集中
语法如下:
select 字段1,字段2… from 表名 where 条件;
例:
select * from students where id=1;
where后面支持多种运算符,进行条件的处理
比较运算
逻辑运算
模糊查询
范围查询
空判断
2、比较运算符
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>
例1:查询小乔的年龄
select age from students where name=’小乔’
例2:查询20岁以下的学生
select * from students where age<20
例3:查询家乡不在北京的学生
select * from students where hometown!=’北京’
练习:
1、查询学号是’007’的学生的身份证号
2、查询’1班’以外的学生信息
3、查询年龄大于20的学生的姓名和性别
逻辑运算符
and
or
not
例1:查询年龄小于20的女同学
select * from students where age<20 and sex=’女’
例2:查询女学生或’1班’的学生
select * from students where sex=’女’ or class=’1班’
例3:查询非天津的学生
select * from students where not hometown=’天津’
练习:
1、查询河南或河北的学生
2、查询’1班’的’上海’的学生
3、查询非20岁的学生
3、模糊查询
like
%表示任意多个任意字符
_表示一个任意字符
例1:查询姓孙的学生
select * from students where name like ‘孙%’
例2:查询姓孙且名字是一个字的学生
select * from students where name like ‘孙_’
例3:查询叫乔的学生
select * from students where name like ‘%乔’
例4:查询姓名含白的学生
select * from students where name like ‘%白%’
练习:
1、查询姓名为两个字的学生
2、查询姓百且年龄大于20的学生
3、查询学号以1结尾的学生
4、范围查询
in表示在一个非连续的范围内
例1:查询家乡是北京或上海或广东的学生
select * from students where hometown in(‘北京’,’上海’,’广东’)
between ... and ...表示在一个连续的范围内
例2:查询年龄为18至20的学生
select * from students where age between 18 and 20
练习:
1、查询年龄在18或19或22的女生
2、查询年龄在20到25以外的学生
5、空判断
注意:null与''是不同的
判空is null
例1:查询没有填写身份证的学生
select * from students where card is null
判非空is not null
例2:查询填写了身份证的学生
select * from students where card is not null
posted @
2019-10-26 10:01
侯小飞
阅读(
1164)
评论()
编辑
收藏
举报
点击右上角即可分享
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步