Table of contents
TOC
折叠目录
展开目录

Like 运算符 (Microsoft Access SQL)Like Operator (Microsoft Access SQL)

office 365 dev account|上次更新日期: 2018/4/3
|
2 参与人员

适用于: Access 2013 | Access 2016Applies to: Access 2013 | Access 2016

本文在语法说明示例In this articleSyntaxRemarks Example

将字符串表达式与 SQL 表达式中的模式进行比较。Compares a string expression to a pattern in an SQL expression.

语法Syntax

表达式"模式"expressionLike " pattern "

运算符语法包含以下成分:The Like operator syntax has these parts:

部分Part说明Description
expressionexpressionWHERE 子句中使用的 SQL 表达式。SQL expression used in a WHERE clause.
模式pattern字符串或字符字符串与_表达式_进行比较。String or character string literal against which expression is compared.

备注Remarks

可以使用Like运算符来查找与所指定的模式相匹配的字段值。You can use the Like operator to find values in a field that match the pattern you specify.对于_模式_,您可以指定的完整值 (例如, Like "Smith"),或您可以使用通配符来查找某个范围的值 (例如,),或者您可以使用通配符来查找某个范围的值 (例如, Like "Sm*")For pattern, you can specify the complete value (for example, Like "Smith"), or you can use wildcard characters to find a range of values (for example, ), or you can use wildcard characters to find a range of values (for example, Like "Sm*").

在表达式中,可以使用Like运算符来比较字段值与字符串表达式。In an expression, you can use the Like operator to compare a field value to a string expression.例如,如果您输入Like "C*"在 SQL 查询中,查询将返回所有以字母 C 开头的字段值在参数查询中,您可以提示用户输入模式搜索。For example, if you enter Like "C*" in an SQL query, the query returns all field values beginning with the letter C. In a parameter query, you can prompt the user for a pattern to search for.

以下示例返回以字母 P 开头并且后面为 A 到 F 之间任何字母和三个数字的数据:The following example returns data that begins with the letter P followed by any letter between A and F and three digits:

Like "P[A-F]###"

下表演示如何使用Like来测试不同模式的表达式。The following table shows how you can use Like to test expressions for different patterns.

* * 种匹配 * * Kind of match*** * 模式 * * Pattern**(返回 True) 的匹配Match (returns True)(返回 False) 不匹配No match (returns False)
多个字符Multiple charactersa*aa*aaa、aBa 和 aBBBaaa, aBa, aBBBaaBCaBC
abababc、AABB 和 Xababc, AABB, XabaZb 和 bacaZb, bac
特殊字符Special charactera[*]aa[*]aa*aa*aaaaaaa
多个字符Multiple charactersab*ab*abcdefg 和 abcabcdefg, abccab 和 aabcab, aab
单个字符Single charactera?aa?aaaa、a3a 和 aBaaaa, a3a, aBaaBBBaaBBBa
单个数字Single digita#aa#aa0a、a1a 和 a2aa0a, a1a, a2aaaa 和 a10aaaa, a10a
字符范围Range of characters[a-z][a-z]f、p 和 jf, p, j2,&;2, &;
范围之外Outside a range[!a-z]9, &;,%9, &;, %b 和 ab, a
非数字值Not a digit[! 0-9][!0-9]A、 a &;,~A, a, &;, ~0、 1、 90, 1, 9
复合值Combineda[!b-m]#a[!b-m]#An9、az0 和 a99An9, az0, a99abc 和 aj0abc, aj0

示例Example

以下示例返回姓名以字母 A 到 D 中的任意一字母开头的雇员列表。This example returns a list of employees whose names begin with the letters A through D.

以下示例调用 EnumFields 过程,您可以在 SELECT 语句示例中找到该过程。This example calls the EnumFields procedure, which you can find in the SELECT statement example.

Sub LikeX() 

    Dim dbs As Database, rst As Recordset 

    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 

    ' Return a list of employees whose names begin with 
    ' the letters A through D. 
    Set rst = dbs.OpenRecordset("SELECT LastName," _ 
        &; " FirstName FROM Employees" _ 
        &; " WHERE LastName Like '[A-D]*';") 

    ' Populate the Recordset. 
    rst.MoveLast 

    ' Call EnumFields to print the contents of the  
    ' Recordset. Pass the Recordset object and desired 
    ' field width. 
    EnumFields rst, 15 

    dbs.Close 

End Sub

访问支持资源ACCESS SUPPORT RESOURCES
访问 MSDN 上的开发人员论坛Access for developers forum on MSDN
访问 support.office.com 上的帮助Access help on support.office.com
访问 answers.microsoft.com 上的帮助Access help on answers.microsoft.com
在必应上搜索特定的访问错误代码Search for specific Access error codes on Bing
访问 UtterAccess 上的论坛Access forums on UtterAccess
访问 UtterAcess 上的 wikiAccess wiki on UtterAcess
访问开发人员和 VBA 编程帮助中心 (FMS)Access developer and VBA programming help center (FMS)
访问 StackOverflow 上的帖子Access posts on StackOverflow

© 2018 Microsoft