MYSQL必知必會-where語句

6章過濾數據

6.1使用WHERE子句

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

6.2WHERE子句操做符

操做符spa

說明ci

=產品

等於table

< >im

不等於數據

=tab

不等於錯誤

<ab

小於

< =

小於等於

>

大於

> =

大於等於

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子句中的子句的關鍵字,也稱爲邏輯操做符。

相關文章
相關標籤/搜索