4.3、where条件句子
作用:检索数据中符合条件的值
搜索的条件由一个或者多个表达式组成!结果 布尔值
逻辑运算符
语法 | 描述 |
---|---|
and && | 两个真,结果为真 |
or // | 其中一个真,结果为真 |
not ! | 真假互换 |
使用where筛查数据
– 使用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 stdentresult>=95 && studentresult<=100 – &&
SELECT studentno,studnetresult
FROM result
WHERE studentresult BETWEEN 95 AND 100 – 模糊查询(区间)
– 除了1000号学生之外的同学的成绩
SELECT studentno,studentresult FROM result
WHERE studentno!=1000 – !
SELECT studentno,studentresult FROM result
WHERE NOT studentno = 1000 – NOT
模糊查询:比较运算符(关键字查询)
-- ============================ 模糊查询 =========================
-- like结合 %(代表0到任意个字符) _(一个字符)
-- 查询性赵的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE studentname LIKE '赵%'
-- 查询姓张的同学,名字后面只有一个字的
SELECT `studentno`,`studentname` FROM `student`
WHERE studentname LIKE '张_'
-- 查询姓刘的同学,名字后面只有两个字的
SELECT `studntno`,`studentname` FROM ``studnt
WHERE studentname LIKE '刘__'
-- 查询名字中带有华字的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE studentname LIKE '%华%'
-- in (具体的一个或者多个值)
-- 查询 1000,1001,1002号学员
SELECT `studentno`,`studentname` FROM `student`
WHERE studentno IN (1000,1001,1002);
-- 查询在北京朝阳的学生
SELECT `studentno`,`studentname`,`address` 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
-- 查询没有出生日期的同学
SELECT `studentno`,`studentname` FROM `student`
WHERE borndate IS NULL