发布于 2015-07-25 11:38:45 | 201 次阅读 | 评论: 0 | 来源: 网络整理
SQLite的GLOB运算符用于对图案使用通配符只匹配文本值。如果搜索表达式可以匹配的图案表达,GLOB运算将返回true,也就是1。 LIKE操作符不同,GLOB是大小写敏感的,它遵循以下通配符指定的UNIX语法。
星号 (*)
问号(?)
星号表示零个或多个数字或字符。?代表一个单一的数字或字符。
*和?基本语法如下:
SELECT FROM table_name
WHERE column GLOB 'XXXX*'
or
SELECT FROM table_name
WHERE column GLOB '*XXXX*'
or
SELECT FROM table_name
WHERE column GLOB 'XXXX?'
or
SELECT FROM table_name
WHERE column GLOB '?XXXX'
or
SELECT FROM table_name
WHERE column GLOB '?XXXX?'
or
SELECT FROM table_name
WHERE column GLOB '????'
可以结合的条件下使用N多的AND或OR运算。这里XXXX可能是任何数字小或字符串值。
这里有数量的例子显示WHERE部分具有不同的LIKE子句有'*'和'?'运算符:
语句 | 描述 |
---|---|
WHERE SALARY GLOB '200*' | Finds any values that start with 200 |
WHERE SALARY GLOB '*200*' | Finds any values that have 200 in any position |
WHERE SALARY GLOB '?00*' | Finds any values that have 00 in the second and third positions |
WHERE SALARY GLOB '2??' | Finds any values that start with 2 and are at least 3 characters in length |
WHERE SALARY GLOB '*2' | Finds any values that end with 2 |
WHERE SALARY GLOB '?2*3' | Finds any values that have a 2 in the second position and end with a 3 |
WHERE SALARY GLOB '2???3' | Finds any values in a five-digit number that start with 2 and end with 3 |
让我们举一个实际的例子,考虑公司表有以下记录:
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
以下是一个例子,它会显示从COMPANY 表年龄以2开始的所有记录:
sqlite> SELECT * FROM COMPANY WHERE AGE GLOB '2*';
这将产生以下结果:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
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
以下是一个例子,它会显示所有记录从COMPANY 表的ADDRESS将有一个连字符( - ),里面的文字:
sqlite> SELECT * FROM COMPANY WHERE ADDRESS GLOB '*-*';
这将产生以下结果:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
4 Mark 25 Rich-Mond 65000.0
6 Kim 22 South-Hall 45000.0