📜  SQLite LIKE子句

📅  最后修改于: 2020-11-12 09:55:29             🧑  作者: Mango

SQLite LIKE子句(运算符)

SQLite LIKE运算符用于使用通配符将文本值与模式匹配。如果搜索表达式与模式表达式匹配,则LIKE运算符将返回true,即1。

有两个通配符与LIKE运算符一起使用:

  • 百分号(%)
  • 下划线(_)

百分号代表零个,一个或多个数字或字符。下划线表示单个数字或字符。

句法:

SELECT FROM table_name
WHERE column LIKE 'XXXX%'

要么

SELECT FROM table_name
WHERE column LIKE '%XXXX%'

要么

SELECT FROM table_name
WHERE column LIKE 'XXXX_'

要么

SELECT FROM table_name
WHERE column LIKE '_XXXX'

要么

SELECT FROM table_name
WHERE column LIKE '_XXXX_'

在此,XXXX可以是任何数字或字符串值。

例:

我们有一个名为“ STUDENT”的表,其中包含以下数据:

在这些示例中,具有不同的LIKE子句的WHERE语句使用'%'和'_'运算符,并且对'FEES'进行了操作:

Statement Description
Where FEES like ‘200%’ It will find any values that start with 200.
Where FEES like ‘%200%’ It will find any values that have 200 in any position.
Where FEES like ‘_00%’ It will find any values that have 00 in the second and third positions.
Where FEES like ‘2_%_%’ It will find any values that start with 2 and are at least 3 characters in length.
Where FEES like ‘%2’ It will find any values that end with 2
Where FEES like ‘_2%3’ It will find any values that have a 2 in the second position and end with a 3
Where FEES like ‘2___3’ It will find any values in a five-digit number that start with 2 and end with 3

示例1:从STUDENT表中选择所有记录,其中年龄从2开始。

SELECT * FROM STUDENT WHERE AGE  LIKE '2%'; 

输出:

范例2:

从STUDENT表中选择所有记录,其中文本内将带有“ a”(a):

SELECT * FROM STUDENT WHERE ADDRESS LIKE '%a%'; 

输出: