MySQL必知必會六 過濾資料

2021-10-06 10:49:05 字數 4103 閱讀 2612

只檢索所需資料需要指定搜尋條件( search criteria) ,搜尋條件也稱為過濾條件( filter condition)。

select語句中,資料根據where子句中指定的搜尋條件進行過濾。where子句在表名(from子句)之後給出,如下所示:

mysql>

select prod_name, prod_price from products where prod_price =

2.50;+

---------------+------------+

| prod_name | prod_price |

+---------------+------------+

| carrots |

2.50

|| tnt (

1 stick)

|2.50|+

---------------+------------+

where子句的位置:在同時使用order by和where子句時,應該讓order by位於where之後, 否則將會產生錯誤

操作符說明=

等於<>

不等於!=

不等於<

小於<=

小於等於

>

大於》=

大於等於

between

在指定的兩個值之間

mysql>

select prod_name, prod_price from products where prod_name =

'fuses';+

-----------+------------+

| prod_name | prod_price |

+-----------+------------+

| fuses |

3.42|+

-----------+------------+

現在來看幾個使用其他操作符的例子。第乙個例子是列出**小於10美元的所有產品:

mysql>

select prod_name, prod_price from products where prod_price <10;

+---------------+------------+

| prod_name | prod_price |

+---------------+------------+

|.5 ton anvil |

5.99||

1 ton anvil |

9.99

|| carrots |

2.50

|| fuses |

3.42

|| oil can |

8.99

|| sling |

4.49

|| tnt (

1 stick)

|2.50|+

---------------+------------+

mysql>

select prod_name, prod_price from products where prod_price <=10;

+----------------+------------+

| prod_name | prod_price |

+----------------+------------+

|.5 ton anvil |

5.99||

1 ton anvil |

9.99

|| bird seed |

10.00

|| carrots |

2.50

|| fuses |

3.42

|| oil can |

8.99

|| sling |

4.49

|| tnt (

1 stick)

|2.50

|| tnt (

5 sticks)

|10.00|+

----------------+------------+

以下例子列出不是由**商1003製造的所有產品:

mysql>

select vend_id, prod_name from products where vend_id <>

1003;+

---------+--------------+

| vend_id | prod_name |

+---------+--------------+

|1001

|.5 ton anvil |

|1001

|1 ton anvil |

|1001

|2 ton anvil |

|1002

| fuses |

|1002

| oil can |

|1005

| jetpack 1000||

1005

| jetpack 2000|+

---------+--------------+

何時使用引號:如果仔細觀察上述where子句中使用的條件,會看到有的值括在單引號內(如前面使用的』fuses』),而有的值未括起來。單引號用來限定字串。如果將值與字串型別的列進行比較,則需要限定引號。用來與數值列進行比較的值不用引號。

下面是相同的例子,其中使用!=而不是<>操作符:

mysql>

select vend_id, prod_name from products where vend_id !=

1003

;

between操作符需要兩個值,即範圍的開始值和結束值。

下面的例子說明如何使用between操作符,它檢索**在5美元和10美元之間的所有產品:

mysql>

select prod_name, prod_price from products where prod_price between

5and10;

+----------------+------------+

| prod_name | prod_price |

+----------------+------------+

|.5 ton anvil |

5.99||

1 ton anvil |

9.99

|| bird seed |

10.00

|| oil can |

8.99

|| tnt (

5 sticks)

|10.00|+

----------------+------------+

null 無值(no value),它與字段包含0、空字串或僅僅包含空格不同。

select語句有乙個特殊的where子句,可用來檢查具有null值的列。這個where子句就是is null子句。其語法如下:

mysql>

select cust_id from customers where cust_email is

null;+

---------+

| cust_id |

+---------+

|10002||

10005|+

---------+

MySQL必知必會 過濾資料

基礎過濾 包括以下內容 面向已知值,進行過濾 where操作符的使用 非組合過濾 組合過濾 where 表示過濾條件 基礎語法 select field from tb where condition 要點 1.面向被檢索的所有資料 並非對查詢結果進行過濾 2.可以根據非檢索列進行過濾 建議實踐 2...

mysql過濾 MySQL必知必會 資料過濾

組合where子句 1.1 and操作符 1.2 or操作符 1.3 計算次序 in 操作符 not操作符 1.1 and操作符 mariadb test select id,age,province from user where age 30 and province 北京 id age pro...

MYSQL必知必會(三)過濾資料

資料過濾 用萬用字元進行過濾 使用萬用字元技巧 在select語句中,資料根據where子句中指定的搜尋條件進行過濾。where子句在表名 from子句 之後給出 select prod name,prod price from products where prod price 2.5 order...