數據庫中的條件查詢


相關運算符

條件查詢需要用到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 '%\_%';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM