1. 程式人生 > >BETWEEN 操作符

BETWEEN 操作符

數據 acc site spa country date HERE sites pre

BETWEEN 操作符選取介於兩個值之間的數據範圍內的值。這些值可以是數值、文本或者日期。

SQL BETWEEN 語法

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

舉例:

選取 alexa 介於 1 和 20 之間的所有網站

SELECT * FROM Websites
WHERE alexa BETWEEN 1 AND 20;

NOT BETWEEN 操作符實例

//選取 alexa 介於 1 和 20 之間的所有網站
SELECT
* FROM Websites WHERE
alexa NOT BETWEEN 1 AND 20;

帶有IN的BETWEEN操作符實例

//選取alexa介於 1 和 20 之間但 country 不為 USA 和 IND 的所有網站
SELECT
* FROM Websites WHERE (alexa BETWEEN 1 AND 20) AND NOT country IN (USA, IND);

帶有文本值的BETWEEN操作符實例

//選取 name 以介於 ‘A‘ 和 ‘H‘ 之間字母開始的所有網站
SELECT
* FROM Websites WHERE name BETWEEN A AND H;

帶有文本值的 NOT BETWEEN 操作符實例

//下面的 SQL 語句選取 name 不介於 AH 之間字母開始的所有網站:
SELECT * FROM Websites
WHERE name NOT BETWEEN A AND H;

帶有日期值的BETWEEN操作符實例

//選取 date 介於 2016-05-102016-05-14 之間的所有訪問記錄:
SELECT * FROM access_log
WHERE date BETWEEN 2016-05-10 AND 2016-05-14;

BETWEEN 操作符