语法:
select 查询列表 from 表名;
where 筛选条件;
筛选条件分类:
1、 按条件运算符分类:> ; < ; = ; ! ; = ; <> ; >= ; <=
# 查数学成绩大于90的学生信息
select * from students
where Math_score>90;
# 查询小组编号不等于5的学生姓名和小组编号
select name,group_id
from students
where group_id!=5;
# 查询学生表中,小组编码group_id不为‘1’的学生信息
select * from students
where group_id<>'1';
2、 按逻辑表达式分类: && ; || ; ! ; and ; or ; not
作用:用于连接条件表达式
&&和and:若俩个条件都为turn,结果就为true,反之为false
||和or:只要有一个条件为true,结果就为true,反之为false
!和not:如果连接的条件本身为false,结果则为true,反之为false
# 查询总成绩在300到400之间的学生姓名和政治、英语、数学、专业课成绩
select name,Politics_score,English_score,Math_score,Major_score
from students
where sum_score>=300 and sum_score<=400;
# 查询总成绩在300到400之间或者高于450的学生姓名和政治、英语、数学、专业课成绩
select name,Politics_score,English_score,Math_score,Major_score
from students
where sum_score>=300 and sum_score<=400 or sum_score>=450;
3、 模糊查询:like ; between and ; in ; is null
特点:一般和通配符(%、_)搭配使用,
若通配符本身为查找对象,需要在其前面加’'转义符
或在语句后面使用(ESCAPE ‘转义符’)指定转义符
# 查询学生名中包含字符a的学生信息
select * from students
where last_name like '%a%'; # %为通配符,表示任意多个字符,包含0个,但不包含null
# 查询学生名中第三个字符为a,第五个字符为g的学生名和总成绩
select last_name,sum_score from students
where last_name like '__a_g%'; # _为通配符
# 查询学号在10到20之间的学生信息
select * from students
where stu_id>=10 and stu_id<=20;
# ----------等价于--------------
select * from students
where stu_id between 10 and 20; # 包含临界值,但大小顺序不能换
# 查询学生的分组编号是1、3、6的学生信息
select * from students
where group_id='1' or group_id='3' or group_id='6';
# ----------等价于--------------
select * from students
where group_id in('1', '3', '6'); # 判断字段内值是否属于in列表中的值
# 查询没有获得奖学金的学生信息
select * from student
where scholarship is null; # is是为了解决=不能判断null的情况
# 查询获得奖学金的学生信息
select * from student
where scholarship is not null;
4、安全等于 <=> :
可以解决=不能判断null,和is不能判断数值型数据的情况
# 查询没有获得奖学金的学生信息
select * from student
where scholarship <=> null;
# 查询总成绩为350的学生信息
select * from students
where sum_scor<=>350;