MySQL where condition clause query

where conditional clause

The search condition can be composed of one or more logical expressions, and the result is generally a Boolean value

Logical Operators

Operator grammar description
and && a and b a && b Logical and two are true, the result is true
or || a or b a || b Logical or one is true, the result is true
not ! not a !a Logic is not true for false, false is true
-- ==========  where  ============
SELECT `name`,`sex` FROM student

-- 查询 name 数值在 95 ~ 100 之间的
SELECT `NAMe`, `address` FROM student WHERE `NAME` >= 95 AND `NAME`<= 100
-- and  &&
SELECT `NAMe`, `address` FROM student WHERE `NAME` >= 95 && `NAME` <= 100
-- between and (区间)
SELECT `name`,`address` FROM student WHERE `name` BETWEEN 95 AND 100

-- 查询name 不等于 1 的同学
SELECT `NAMe`, `address` FROM student WHERE `NAME` != 1 AND `NAME` < 10
-- not 
SELECT `NAMe`, `address` FROM student WHERE NOT `NAME` = 100 AND `NAME` > 90


Fuzzy query: comparison operators

Operator grammar description
is null a is null If a is null, the result is true
is not null a is not null If a is not null, the result is true
between and 3 between 1 and 5 If 3 is between 1 and 5, the result is true
Like a like b sql matches, if a matches b, the result is true
In a in (abcd, pdosa, …) If a is in (abcd, pdosa, …), the result is true
-- ==========  模糊查询  ============
-- 查询 姓张的  like 结合  %(代表0到任意个字符)  _(代表一个字符)
SELECT `name` FROM student WHERE `name` LIKE '张%'
-- 查询 姓张的 后面只有一个字的
SELECT `name` FROM student WHERE `name` LIKE '张_'
-- 查询 姓张的 后面有两个字的
SELECT `name` FROM student WHERE `name` LIKE '张__'
-- 查询 名字中有张字的
SELECT `name` FROM student WHERE `name` LIKE '%张%'


-- ==== in 具体的一个或多个值 ====
-- 查询 1,2,3 号同学
SELECT `id`,`name` FROM student WHERE `id` IN (1,2,3)
-- 查询 北京 的同学
SELECT `id`,`name`,`address` FROM student WHERE `address` IN ('北京')

-- ==== null , not null ====
-- 查询地址为空或者null的
SELECT `name`,`address` FROM student WHERE `address`='' OR `address` IS NULL

-- 查询有日期的  不为空的
SELECT `name`,`address`,`birthday` FROM student WHERE `birthday` IS NOT NULL

-- 查询没有日期的  为空的
SELECT `name`,`address`,`birthday` FROM student WHERE `birthday` IS NULL

Guess you like

Origin blog.csdn.net/weixin_44953227/article/details/108737622