SQLite 邏輯運算符
這裡是在SQLite中所有的邏輯運算符一個列表
運算符 | 描述 |
---|---|
AND | The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause. |
BETWEEN | The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value. |
EXISTS | The EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria. |
IN | The IN operator is used to compare a value to a list of literal values that have been specified. |
NOT IN | The negation of IN operator which is used to compare a value to a list of literal values that have been specified. |
LIKE | The LIKE operator is used to compare a value to similar values using wildcard operators. |
GLOB | The GLOB operator is used to compare a value to similar values using wildcard operators. Also, GLOB is case sensitive, unlike LIKE. |
NOT | The NOT operator reverses the meaning of the logical operator with which it is used. Eg. NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is negate operator. |
OR | The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause. |
IS NULL | The NULL operator is used to compare a value with a NULL value. |
IS | The IS operator work like = |
IS NOT | The IS operator work like != |
|| | Adds two different strings and make new one. |
UNIQUE | The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates). |
示例
考慮COMPANY 表有以下記錄:
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語句列出了所有的記錄AGE 大於或等於25和工資大於或等於65000.00:
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
下麵的SELECT語句列出了所有的記錄年齡大於或等於25或薪金大於或等於65000.00
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
下麵的SELECT語句列表年齡不為NULL,這意味著所有記錄下來的所有記錄,因為記錄是冇有年齡等於NULL:
sqlite> SELECT * FROM COMPANY WHERE AGE IS NOT NULL; 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
下麵的SELECT語句列出了所有的記錄名稱以 'Ki' 開頭, 無所謂什麼在 'Ki' 的後麵.
sqlite> SELECT * FROM COMPANY WHERE NAME LIKE 'Ki%'; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 6 Kim 22 South-Hall 45000.0
下麵的SELECT語句列出了所有的記錄名稱以 'Ki' 開頭, 無所謂什麼在 'Ki' 的後麵.
sqlite> SELECT * FROM COMPANY WHERE NAME GLOB 'Ki*'; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 6 Kim 22 South-Hall 45000.0
下麵的SELECT語句列表年齡值是25或27的所有記錄:
sqlite> SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 ); ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 2 Allen 25 Texas 15000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
下麵的SELECT語句列表年齡值既不是25也不是27的所有記錄:
sqlite> SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 ); ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 3 Teddy 23 Norway 20000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
下麵的SELECT語句列出了所有的年齡值是在25和27之間的記錄:
sqlite> SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 2 Allen 25 Texas 15000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0
下麵的SELECT語句使用SQL子查詢子查詢發現所有記錄年齡字段的SALARY>65000,後跟 WHERE子句一起使用EXISTS操作列出了所有的記錄存在年齡從外麵查詢子查詢返回的結果:
sqlite> SELECT AGE FROM COMPANY WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY > 65000); AGE ---------- 32 25 23 25 27 22 24
下麵的SELECT語句使用SQL子查詢子查詢發現所有記錄年齡場有SALARY>65000後正在使用WHERE子句>運算符一起列出了所有的記錄,其中從外部查詢年齡大於年齡在子查詢返回的結果:
sqlite> SELECT * FROM COMPANY WHERE AGE > (SELECT AGE FROM COMPANY WHERE SALARY > 65000); ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0