作用:检索数据中符合条件
的值
注意:搜索的条件由一个或者多个表达式组成!结果 布尔值
运算符 | 语法 | 描述 |
---|---|---|
and && | a and b a&&b | 逻辑与,两个都为真,结果为真 |
or || | a or b a|| b | 逻辑或,其中一个为真,则结果为真 |
Not ! | not a !a | 逻辑非, 真为假,假为真! |
注意:尽量使用英文
-- =================== where ======================
SELECT studentNo,`StudentResult` FROM result
-- 查询考试成绩在 95~100 分之间
SELECT studentNo,`StudentResult` FROM result
WHERE StudentResult>=95 AND StudentResult<=100
-- and &&
SELECT studentNo,`StudentResult` FROM result
WHERE StudentResult>=95 && StudentResult<=100
-- 除了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 | 如果操作符不为 null,结果为真 |
BETWEEN | a between b and c | 若a 在 b 和c 之间,则结果为真 |
Like | a like b | SQL 匹配,如果a匹配b,则结果为真 |
In | a in (a1,a2,a3….) | 假设a在a1,或者a2…. 其中的某一个值中,结果为真 |
%
)-- 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 '刘__'
效果:
%嘉%
)SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '%嘉%'
效果:
具体的一个或者多个值
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentNo IN (1001,1002,1003);
效果:
SELECT `StudentNo`,`StudentName`,`address`FROM `student`
WHERE `address` IN ('河南洛阳')
效果:
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE address='' OR address IS NULL
-- 查询出来是为空
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `BornDate` IS NULL
-- 查询出来也是空
SELECT `StudentNo`,`StudentName`,`BornDate`FROM `student`
WHERE `BornDate` IS NOT NULL
效果:
SELECT studentNo,`StudentResult` FROM result
WHERE StudentResult BETWEEN 95 AND 100
效果:
原文:https://www.cnblogs.com/godles/p/12205838.html