数据库中的条件查询

相关运算符

条件查询需要用到where语句,where必须放到from语句表的后面。

运算符 说明
= 等于
<> 或 != 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
between...and... 两值之间,等同于>=and<=
is null 为null(is not null 不为空)
and 并且
or 或者
in 包含,相当于多个or(not in不在这个范围中)
not not可以取非,主要用在is或in中
like like称为模糊查询,支持%或下划线匹配.
示例:

1、在boot_crm中的customer表查找cust_id为25的cust_name。
在这里插入图片描述
2、在boot_crm中的customer表查找cust_name为Tom的cust_id。
在这里插入图片描述
3、在boot_crm中的customer表查找cust_id大于25的。
在这里插入图片描述
4、在boot_crm中的customer表查找cust_id在20和30之间的。
在这里插入图片描述
5、and和or联合使用,and优先级高,在个别问题中优先级不确定的话,需要加小括号。如:

SELECT cust_name, cust_create_id,cust_industry 
FROM boot_crm.customer
where cust_create_id = 1 and 
(cust_industry = 1 or cust_industry = 2);

在这里插入图片描述
6、in 包含,相当于多个or(not in不在这个范围中)。
找出客户为成功客户的和潜在客户的:

SELECT dict_type_name, dict_item_name 
FROM boot_crm.base_dict
where dict_item_name 
in('成功客户', '潜在客户');

在这里插入图片描述
7、模糊查询like中有两个特殊字符:%代表任意多个字符;_代表任意一个字符。
找出名字里面带有“小”的:

SELECT * FROM boot_crm.customer
where cust_name like '%小%';

在这里插入图片描述
找出名字第二个字是“明”的:

SELECT * FROM boot_crm.customer
where cust_name like '_明%';

在这里插入图片描述
如果要找带有下划线的:(转义字符)

SELECT * FROM boot_crm.customer
where cust_name like '%\_%';
原文地址:https://www.cnblogs.com/yu011/p/13253313.html