4.3、where 条件子句
作用:检索数据中符合条件
的值
搜索的条件由一个或者多个表达式组成!结果为布尔值
逻辑运算符
运算符 | 语法 | 描述 |
---|---|---|
and && | a and b a&&b | 逻辑与,两个为真,则为真 |
or || | 逻辑或,一个为真,则为真 | |
Not ! | 逻辑非,真为假,假为真 |
-- -------where----- --
-- 查询所有学员的成绩
SELECT `studentno`,`studentresult` FROM `result`
-- 查询成绩在70到90之间的学生
-- and
SELECT `studentno`,`studentresult` FROM `result`
WHERE `studentresult`>=70 AND `studentresult`<=90;
-- &&
SELECT `studentno`,`studentresult` FROM `result`
WHERE `studentresult`>=70 && `studentresult`<=90;
-- 查询除了1000号之外的学生的成绩
-- !
SELECT `studentno`,`studentresult` FROM `result`
WHERE `studentno` !=1000;
-- Not
SELECT `studentno`,`studentresult` FROM `result`
WHERE NOT `studentno` =1000;
模糊查询:比较运算符
运算符 | 语法 | 描述 |
---|---|---|
is null | a is null | 如果操作符null,则为真 |
is not null | a is not null | 如果操作符为 not null ,则为真 |
between | a between b and c | 若a在b和c之间,则为真 |
like | a like b | SQL匹配,如果a匹配b,则结果为真 |
in | a in(a2,a3,a4…) | 假设a在a2,a3…其中的某一个值中,则结果为真 |
-- -------模糊查询----- --
-- like %(代表0到任意个字符) _(一个字符)
-- 查询姓张的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE `studentname` LIKE '张%';
-- 查询两个字的张姓同学
SELECT `studentno`,`studentname` FROM `student`
WHERE `studentname` LIKE '张_';
-- 查询张姓同学,名字后面有两个字
SELECT `studentno`,`studentname` FROM `student`
WHERE `studentname` LIKE '张__';
-- 查询1000,1001,1002号同学 用 between
SELECT `studentno`,`studentname` FROM `student`
WHERE `studentno` BETWEEN 1000 AND 1002;
-- 查询1000,1001,1002号同学
-- 用 in (具体的一个或者多个值)
SELECT `studentno`,`studentname` FROM `student`
WHERE `studentno` IN(1000,1001,1002);
-- 查询地址在南昌或者天津的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE `address` IN('南昌','天津');
-- 查询出生日期为空的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE `borndate` IS NULL;
-- 查询出生日期不为空的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE `borndate` IS NOT NULL;