SQL (三)過濾數據(select語句的where子句,指定搜索條件)

注意:不要用應用過濾,而用SQL過濾

因爲

  • SQL纔是操作數據庫最高效的語言,客戶源開發語言操作低效
  • 服務器把大量未經過濾的數據通過網絡傳給客戶端,浪費網絡資源

在這裏插入圖片描述

WHERE 子句(在from子句後)

在這裏插入圖片描述
在這裏插入圖片描述

where子句的條件操作符

好多奇怪的長相
在這裏插入圖片描述
在這裏插入圖片描述

示例1:<, <=

select prod_name, prod_price
from products
where prod_price < 5;

在這裏插入圖片描述

示例2:相等檢驗

檢索兩列,不返回所有行

select prod_name, prod_price
from products
where prod_price = 3.49;

在這裏插入圖片描述

示例3:不匹配檢查

select vend_id, prod_name
from products
where vend_id <>'DLL01';

在這裏插入圖片描述
在這裏插入圖片描述
注意就算是字符串,也是單引號

在這裏插入圖片描述
mysql也支持!=表示不等於

select vend_id, prod_name
from products
where vend_id !='DLL01';

示例4:範圍檢查,between

select prod_name, prod_price
from products
where prod_price between 5 and 10;

在這裏插入圖片描述
在這裏插入圖片描述

示例5:空值檢查(where子句的is null子句)

原來子句還可以有子句
在這裏插入圖片描述

select prod_name
from products
where prod_price is null;

在這裏插入圖片描述

select cust_name
from customers
where cust_email is null;

在這裏插入圖片描述

總結

介紹瞭如何用SELECT語句的WHERE子句過濾返回的數據。

學習瞭如何檢驗相等、不相等、大於、小於、值的範圍以及NULL值等。

關鍵字

  • where
  • between
  • and
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章