作用:检索数据中 符合条件 的值
搜索的条件由一个或者多个表达式组成!结果为布尔值

逻辑运算符

运算符 语法 描述
and && a and b a&&b 逻辑与 两个都为真,结果为真
or || a or b a||b 逻辑或 其中一个为真,则结果为真
Not ! not a !a 逻辑非

尽量使用英文字母

  1. -- 查询考试成绩在95-100之间
  2. SELECT studentNO,`StudentResult` FROM result
  3. WHERE StudentResult>=95 AND StudentResult<=100
  4. -- &&
  5. SELECT studentNO,`StudentResult` FROM result
  6. WHERE StudentResult>=95 && StudentResult<=100
  7. -- 模糊查询(区间)
  8. SELECT studentNO,`StudentResult` FROM result
  9. WHERE StudentResult BETWEEN 95 AND 100
  10. -- 查询除了1000号学生以外的同学的成绩
  11. SELECT StudentNo,`StudentResult` FROM result
  12. WHERE StudentNo != 1000
  13. -- not
  14. SELECT StudentNo,`StudentResult` FROM result
  15. 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 (a1,a2,a3,…..) 假设a在a1 或者a2….其中的某一个值中,结果为真
-- ================================模糊查询========================================

-- 查询姓张的同学
-- like结合 (只有在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 '%松%'

-- ==========in(具体的一个或多个值)===========

-- 查询1001,1002号学员
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentNo IN (1001,1002)

-- 查询在北京的学生
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `Address` IN ('北京朝阳')

-- ==============null    not null====================
-- 查询地址为空的学生
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE address='' OR address IS NULL

-- 查询有出生日期的同学     不为空
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `BornDate` IS NOT NULL