一、SQL入門語句之LIKE
LIKE用來匹配通配符指定模式的文本值。如果搜索表達式與模式表達式匹配,LIKE 運算符將返回真(true),也就是 1。這里有兩個通配符與 LIKE 運算符一起使用,百分號(%)代表零個、一個或多個數字或字符。下划線(_)代表一個單一的數字或字符。這些符號可以被組合使用。
1、查找字段A以AAA開頭的任意值
select * from table_name where 字段A like 'AAA%'
2、查找字段A任意位置包含AAA的任意值
select * from table_name where 字段A like '%AAA%'
3、查找字段A第二位和第三位為 AA 的任意值
select *from table_name where 字段A like '_AA%'
4、查找字段A以 A 開頭,且長度至少為 3 個字符的任意值
select * from table_name where 字段A like 'A_%_%'
5、查找字段A以 A 結尾的任意值
select *from table_name where 字段A like '%A'
6、查找字段A第二位為 A,且以 B 結尾的任意值
select *from table_name where 字段A like '_A%B'
7、查找字段A長度為 5 位數,且以 A 開頭以 B 結尾的任意值(A,B中間三個下划線)
select *from table_name where 字段A like 'A___B'
二、SQL入門語句之GLOB
GLOB是用來匹配通配符指定模式的文本值。如果搜索表達式與模式表達式匹配,GLOB 運算符將返回真(true),也就是 1。與 LIKE 運算符不同的是,GLOB 是大小寫敏感的,通配符有星號(*)代表零個、一個或多個數字或字符。問號(?)代表一個單一的數字或字符。這些符號可以被組合使用,它遵循 UNIX 的語法。
1、查找字段A以AAA開頭的任意值
select * from table_name where 字段A GLOB 'AAA*'
2、查找字段A任意位置包含AAA的任意值
select * from table_name where 字段A GLOB '*AAA*'
3、查找字段A第二位和第三位為 AA 的任意值
select *from table_name where 字段A GLOB '?AA*'
4、查找字段A以 A 開頭,且長度至少為 3 個字符的任意值
select * from table_name where 字段A GLOB 'A?*?*'
5、查找字段A以 A 結尾的任意值
select *from table_name where 字段A GLOB '*A'
6、查找字段A第二位為 A,且以 B 結尾的任意值
select *from table_name where 字段A GLOB '?A*B'
7、查找字段A長度為 5 位數,且以 A 開頭以 B 結尾的任意值(A,B中間三個下划線)
select *from table_name where 字段A GLOB 'A???B'
三、SQL入門語句之LIMIT
LIMIT用於限制由 SELECT 語句返回的數據數量。
1、從數據庫表中獲取 n 條數據
select *from table_name limit n
2、從數據庫表中第 m 條開始獲取 n 條數據
select *from table_name limit n offset m
3、從數據庫表中獲取滿足特定條件的 n 條數據
select *from table_name where [condition] limit n
4、從數據庫表中滿足條件的第 m 條開始獲取 n 條數據
select *from table_name where [condition] limit n offset m