MySQL——Where条件子句

作用:检索数据中符合条件的值

注意:搜索的条件由一个或者多个表达式组成!结果 布尔值

1.1、逻辑运算符

运算符 语法 描述
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

1.2、模糊查询 : 比较运算符

运算符 语法 描述
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…. 其中的某一个值中,结果为真

1.2.1、Like关键字

1、查询姓刘的同学(%

-- like结合  %(代表0到任意个字符)   _(一个字符)
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '刘%'

效果:这里姓刘的同学的名不管有多少字都匹配

2、查询姓刘的同学,名字后面只有一个字的(_

SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '刘_'

效果:

3、查询姓刘的同学,名字后面只有两个字的(__

SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '刘__'

效果:

4、查询名字中间有嘉字的同学 (%嘉%

SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentName LIKE '%嘉%'

效果:

1.2.2、IN关键字

具体的一个或者多个值

1、查询1001,1002,1003号学员

SELECT `StudentNo`,`StudentName` FROM `student`
WHERE StudentNo IN (1001,1002,1003);

效果:

2、查询在河南洛阳的学生

SELECT `StudentNo`,`StudentName`,`address`FROM `student`
WHERE `address` IN ('河南洛阳')

效果:

1.2.3、IS NULL关键字

1、查询地址为空的学生

SELECT `StudentNo`,`StudentName` FROM `student`
WHERE address=''  OR address IS NULL
-- 查询出来是为空

2、查询没有有出生日期的同学

SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `BornDate` IS NULL
-- 查询出来也是空

1.2.4、IS NOT NULL关键字

1、查询有出生日期的同学

SELECT `StudentNo`,`StudentName`,`BornDate`FROM `student`
WHERE `BornDate` IS NOT NULL

效果:

1.2.4、BETWEEN关键字

1、查询考试成绩在 95~100 分之间的学生

SELECT studentNo,`StudentResult` FROM result
WHERE StudentResult BETWEEN 95 AND 100

效果:

猜你喜欢

转载自www.cnblogs.com/godles/p/12205838.html