第6章過濾數據
6.1使用WHERE子句
Select name, price from products where price = 2.50;過濾出products表里price列等於2.50的列(name,price兩列)
6.2WHERE子句操作符
操作符 |
說明 |
= |
等於 |
< > |
不等於 |
!= |
不等於 |
< |
小於 |
< = |
小於等於 |
> |
大於 |
> = |
大於等於 |
BETWEEN |
在指定的兩個值之間 |
6.2.1檢查單個值
Select name, price from products where prod_name = ‘fuses’;按name等於fuses,輸出(name,price兩個列,MySQL執行是不區分大小寫)
Select name, price from products where prod_price < ‘10’;按price小於10的列,輸出(name,price兩個列,MySQL執行是不區分大小寫)
Select name, price from products where prod_price < = ‘10’;按price小於等於10的列,輸出(name,price兩個列,MySQL執行是不區分大小寫)
6.2.2不匹配檢查
排除供應商編號為1003制造的所有產品
Select id, name from products where id < > 1003;
6.2.3范圍值檢查
- between操作符(必須有兩個值,而且用and連接)
Select name, price from products where price between 5 and 10;按price列取出5到10之間的name,price兩列內容
#注:between操作符需要用兩個值(低端值和高端值),切必須用and想連
6.2.4空值檢查
檢查表里name列為空的列
Select id, name, price from products where name is null;
第七章數據過濾
7.1組合where子句
Mysql允許給出多個where子句,子句可以以兩種方式使用;以and子句和or 子句的方式使用
7.1.1and操作符
取出表中id=1003和price< = 10;的數據
Select id, name, price, from products where id = 1003 and price < = 10;
7.1.2or操作符
取出表中id id 等於1002,或id 等於1003的數據
Select id, name, price from products where id = 1002 or id = 1003;
7.1.3計算次序
價格為10元以上,且有1002或1003的所有產品
錯誤的SQL:
Select name, price, from produtes where id = 1002 or id = 1003 and price > = 10;
#因為優先級問題(and優先級大於or的優先級)
正確的SQL:
#注:()的優先級大於and,and的優先級大於or的優先級
Select name, price, from produtes where (id = 1002 or id = 1003) and price > = 10;
7.2什么是操作符?
用來聯絡或改變where子句中的子句的關鍵字,也稱為邏輯操作符。