SQLite 的 LIKE 运算符是用来匹配通配符指定模式的文本值。如果搜索表达式与模式表达式匹配,LIKE 运算符将返回真(true),也就是 1。这里有两个通配符与 LIKE 运算符一起使用:
百分号(%)代表零个、一个或多个数字或字符。下划线(_)代表一个单一的数字或字符。这些符号可以被组合使用。
% 和 _ 的基本语法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<span class="pln">SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'XXXX%'</span> <span class="kwd">or</span><span class="pln"> SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'%XXXX%'</span> <span class="kwd">or</span><span class="pln"> SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'XXXX_'</span> <span class="kwd">or</span><span class="pln"> SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'_XXXX'</span> <span class="kwd">or</span><span class="pln"> SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'_XXXX_'</span> |
您可以使用 AND 或 OR 运算符来结合 N 个数量的条件。在这里,XXXX 可以是任何数字或字符串值。
下面一些实例演示了 带有 '%' 和 '_' 运算符的 LIKE 子句不同的地方:
语句 | 描述 |
---|---|
WHERE SALARY LIKE '200%' | 查找以 200 开头的任意值 |
WHERE SALARY LIKE '%200%' | 查找任意位置包含 200 的任意值 |
WHERE SALARY LIKE '_00%' | 查找第二位和第三位为 00 的任意值 |
WHERE SALARY LIKE '2_%_%' | 查找以 2 开头,且长度至少为 3 个字符的任意值 |
WHERE SALARY LIKE '%2' | 查找以 2 结尾的任意值 |
WHERE SALARY LIKE '_2%3' | 查找第二位为 2,且以 3 结尾的任意值 |
WHERE SALARY LIKE '2___3' | 查找长度为 5 位数,且以 2 开头以 3 结尾的任意值 |
让我们举一个实际的例子,假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面是一个实例,它显示 COMPANY 表中 AGE 以 2 开头的所有记录:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE AGE LIKE </span><span class="str">'2%'</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面是一个实例,它显示 COMPANY 表中 ADDRESS 文本里包含一个连字符(-)的所有记录:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE ADDRESS LIKE </span><span class="str">'%-%'</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0 from:http://www.runoob.com/sqlite/sqlite-like-clause.html</span> |