SQLite - WHERE子句sql
SQLite WHERE子句用於指定一個條件同時抓取數據從一個表或多個表。 spa
若是給定的條件滿意,意味着true,而後從表中返回特定值。你會使用WHERE子句來篩選記錄,只獲取必要的記錄。 code
WHERE子句不只用於SELECT語句,但它也用於更新、刪除語句,等等,咱們將在後續章節的研究。sqlite
語句 it
SQLite SELECT語句的WHERE子句的基本語法以下: io
SELECT column1, column2, columnN table
FROM table_name 語法
WHERE [condition]im
例子 數據
您能夠指定一個條件使用比較或邏輯運算符>、<、=,,等。考慮公司表有如下記錄:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
如下是簡單的例子顯示使用SQLite邏輯運算符。下列SELECT語句列表下的全部記錄,年齡大於或等於25和工資是大於且等於65000.00:
sqlite> SELECT * FROM COMPANY WHERE AGE >=25 AND SALARY >=65000;
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
4Mark25Rich-Mond65000.0
5David27Texas85000.0
下列SELECT語句列表下的全部記錄,年齡大於或等於25或工資是大於或等於65000.00:
sqlite> SELECT * FROM COMPANY WHERE AGE >=25 OR SALARY >=65000;
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
1Paul32California20000.0
2Allen25Texas15000.0
4Mark25Rich-Mond65000.0
5David27Texas85000.0
下列SELECT語句列表下的全部記錄,年齡並非空這意味着全部的記錄,由於沒有記錄的年齡等於零:
sqlite> SELECT * FROM COMPANY WHERE AGE IS NOT NULL;
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
1Paul32California20000.0
2Allen25Texas15000.0
3Teddy23Norway20000.0
4Mark25Rich-Mond65000.0
5David27Texas85000.0
6Kim22South-Hall45000.0
7James24Houston10000.0
下列SELECT語句列表下的全部記錄名稱始於"ki","ki"後是什麼並不重要。
sqlite> SELECT * FROM COMPANY WHERE NAME LIKE 'Ki%';
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
6Kim22South-Hall45000.0
下列SELECT語句列表下的全部記錄名稱始於"ki","ki"後是什麼並不重要。
sqlite> SELECT * FROM COMPANY WHERE NAME GLOB 'Ki*';
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
6Kim22South-Hall45000.0
下列SELECT語句列表下的全部記錄年齡值爲25或27:
sqlite> SELECT * FROM COMPANY WHERE AGE IN (25,27);
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
2Allen25Texas15000.0
4Mark25Rich-Mond65000.0
5David27Texas85000.0
下列SELECT語句列表下的全部記錄的年齡值不是25和27:
sqlite> SELECT * FROM COMPANY WHERE AGE NOT IN (25,27);
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
1Paul32California20000.0
3Teddy23Norway20000.0
6Kim22South-Hall45000.0
7James24Houston10000.0
下列SELECT語句列表下的全部記錄,年齡25 - 27之間:
sqlite> SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27;
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
2Allen25Texas15000.0
4Mark25Rich-Mond65000.0
5David27Texas85000.0
SELECT語句利用SQL查詢,子查詢後發現全部記錄年齡字段有工資> 65000,後來使用where子句以及存在運營商列表下的全部記錄,年齡從外部查詢存在於子查詢返回的結果:
sqlite> SELECT AGE FROM COMPANY
WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY >65000);
AGE
----------
32
25
23
25
27
22
24
SELECT語句利用SQL查詢,子查詢後發現全部記錄年齡字段有工資> 65000,後來一塊兒使用where子句>操做符列表下來年齡之外的全部記錄查詢年齡大於子查詢返回的結果:
sqlite> SELECT * FROM COMPANY
WHERE AGE >(SELECT AGE FROM COMPANY WHERE SALARY >65000);
ID NAME AGE ADDRESS SALARY
--------------------------------------------------
1Paul32California20000.0