MySQL過濾資料 md

2021-08-02 22:24:30 字數 3106 閱讀 7235

23 範圍值查詢

使用where子句,指定搜尋條件。

只檢索所需資料需要指定搜尋條件(search criteria),在使用select語句中,資料根據where子句中指定的條件進行搜尋。如下,

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 |

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

操作符說明=

等於<>

不等於!=

不等於<

小於》大於<=

小於等於

=
大於等於

between

在指定的兩個值之間

mysql> select prod_name, prod_price from products where prod_name = 'fuses';

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

| prod_name | prod_price |

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

| fuses | 3.42 |

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

分析,由於mysql不區分大小寫,所以prod_name值區fuses或fuses是相同的。

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 |

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

例如,不是由**商1003**的產品,

mysql> select prod_name, prod_price from products where vend_id <> 1003;

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

| prod_name | prod_price |

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

| .5 ton anvil | 5.99 |

| 1 ton anvil | 9.99 |

| 2 ton anvil | 14.99 |

| fuses | 3.42 |

| jetpack 1000 | 35.00 |

| jetpack 2000 | 55.00 |

| oil can | 8.99 |

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

使用between操作符,進行範圍查詢。可以檢索數值,或者日期。

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

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

| 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 |

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

說明,betweenand運算元據,包括兩個邊界值

null空值,與字段0、空字元或僅僅空格是不同的。

select語句中,有特色where子句用來檢索空值,is null.

mysql> select prod_name from products where prod_price is null;

empty

set (0.00 sec)

mysql> select cust_id from customers where cust_email is null;

+---------+

| cust_id |

+---------+

| 10002 |

| 10005 |

| 10006 |

| 10007 |

| 10008 |

+---------+

mysql過濾資料 MySQL過濾資料

1.mysql過濾資料 使用where子句 select prod name,prod price from products where prod price 2.50 檢查單個值 select prod name,prod price from products where prod name ...

mysql過濾範圍 MySQL 過濾資料

使用select語句的where子句指定搜尋條件 在select語句中,資料根據where子句中指定的搜尋條件進行過濾,where子句在表名之後給出 select 列名1,列名2 from 表名 where 條件 條件為列中資料的特定值 where子句位置 select from where ord...

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...