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的学生的姓名和性别逻辑运算符andornot例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