萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> sql between 條件語句用法

sql between 條件語句用法

文章利用了幾個實例來介紹關於sql between 條件語句的用法,有需要的朋友可以參考一下這個函數。

BETWEEN 條件,允許您檢索在一定范圍內的值。

BETWEEN的語法是:

 代碼如下 復制代碼

SELECT columns
FROM tables
WHERE column1 between value1 and value2;

實例

 代碼如下 復制代碼

SELECT *
FROM suppliers
WHERE supplier_id between 5000 AND 5010;

當也可以用其它來判斷

 代碼如下 復制代碼

SELECT *
FROM suppliers
WHERE supplier_id >= 5000
AND supplier_id <= 5010;

對日期的操作

實例,求兩日期之間的數據

 代碼如下 復制代碼

SELECT *
FROM orders
WHERE order_date between to_date ('2003/01/01', 'yyyy/mm/dd')
AND to_date ('2003/12/31', 'yyyy/mm/dd');


另一種方法

 代碼如下 復制代碼

SELECT *
FROM orders
WHERE order_date >= to_date('2003/01/01', 'yyyy/mm/dd')
AND order_date <= to_date('2003/12/31','yyyy/mm/dd');


實例三 not between

 代碼如下 復制代碼

SELECT *
FROM suppliers
WHERE supplier_id not between 5000 and 5500;

另一種方法

 代碼如下 復制代碼

SELECT *
FROM suppliers
WHERE supplier_id < 5000
OR supplier_id > 5500;

copyright © 萬盛學電腦網 all rights reserved