MYSQL必知必會-where語句


6章過濾數據

6.1使用WHERE子句

Select  nameprice  from  products  where  price  =  2.50;過濾出products表里price列等於2.50的列(nameprice兩列)

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范圍值檢查

  1. between操作符(必須有兩個值,而且用and連接)

Select  name,  price  from products  where  price  between  5  and 10;price列取出510之間的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=1003price< = 10;的數據

Select  id,  name,  price,  from  products  where  id = 1003 and price < = 10;

7.1.2or操作符

取出表中id id 等於1002,或id 等於1003的數據

Select idname,  price  from products where id = 1002 or id = 1003;

7.1.3計算次序

價格為10元以上,且有10021003的所有產品

錯誤的SQL

Select  namepricefrom  produtes  where  id = 1002 or id = 1003 and price > = 10;

#因為優先級問題(and優先級大於or的優先級)

正確的SQL

#注:()的優先級大於and,and的優先級大於or的優先級

Select  namepricefrom  produtes  where  id = 1002 or id = 1003and price > = 10;

7.2什么是操作符?

用來聯絡或改變where子句中的子句的關鍵字,也稱為邏輯操作符。


免責聲明!

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



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