发布于 2015-07-25 11:47:16 | 240 次阅读 | 评论: 0 | 来源: 网络整理

SQLite的LIKE操作符是用来反对使用通配符的模式匹配的文本值。如果搜索表达式可以匹配的图案表达,LIKE运算将返回true,也就是1。有两个通配符与LIKE运算符一起使用:

  • 百分号 (%)

  • 下划线 (_)

百分号表示零个,一个或多个数字或字符。下划线代表一个单一的数字或字符。这些符号可以被组合使用。

语法

%和_的基本语法如下:


SELECT FROM table_name
WHERE column LIKE 'XXXX%'

or 

SELECT FROM table_name
WHERE column LIKE '%XXXX%'

or

SELECT FROM table_name
WHERE column LIKE 'XXXX_'

or

SELECT FROM table_name
WHERE column LIKE '_XXXX'

or

SELECT FROM table_name
WHERE column LIKE '_XXXX_'

可以结合的条件下使用AND或OR运算的N多。XXXX在这里可以是任何数字或字符串值。

例子:

下面一些例子显示LIKE子句以'%'和'_'运算符具有不同的部分:

语句 描述
WHERE SALARY LIKE '200%' Finds any values that start with 200
WHERE SALARY LIKE '%200%' Finds any values that have 200 in any position
WHERE SALARY LIKE '_00%' Finds any values that have 00 in the second and third positions
WHERE SALARY LIKE '2_%_%' Finds any values that start with 2 and are at least 3 characters in length
WHERE SALARY LIKE '%2' Finds any values that end with 2
WHERE SALARY LIKE '_2%3' Finds any values that have a 2 in the second position and end with a 3
WHERE SALARY LIKE '2___3' Finds any values in a five-digit number that start with 2 and end with 3

让我们举一个实际的例子,考虑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

以下是一个例子,它会从公司表显示年龄以2开头 的所有记录:


sqlite> SELECT * FROM COMPANY WHERE AGE  LIKE '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

以下是一个例子,它会显示所有记录从公司表的地址将有一个连字符( - ),里面的文字:


sqlite> SELECT * FROM COMPANY WHERE ADDRESS  LIKE '%-%';

这将产生以下结果:


ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
4           Mark        25          Rich-Mond   65000.0
6           Kim         22          South-Hall  45000.0
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务