12.4.2 Comparison Functions and Operators比较函数和运算符

Table 12.4 Comparison Operators比较运算符

Name名称Description描述
>Greater than operator大于运算符
>=Greater than or equal operator大于等于运算符
<Less than operator小于运算符
<>, !=Not equal operator不等运算符
<=Less than or equal operator小于等于运算符
<=>NULL-safe equal to operatorNULL安全等于运算符
=Equal operator相等运算符
BETWEEN ... AND ...Whether a value is within a range of values值是否在值的范围内
COALESCE()Return the first non-NULL argument返回第一个非空参数
GREATEST()Return the largest argument返回最大参数
IN()Whether a value is within a set of values值是否在一组值内
INTERVAL()Return the index of the argument that is less than the first argument返回小于第一个参数的参数索引
ISTest a value against a boolean根据布尔值测试值
IS NOTTest a value against a boolean根据布尔值测试值
IS NOT NULLNOT NULL value testNOT NULL值测试
IS NULLNULL value testNULL值测试
ISNULL()Test whether the argument is NULL测试参数是否为NULL
LEAST()Return the smallest argument返回最小参数
LIKESimple pattern matching简单模式匹配
NOT BETWEEN ... AND ...Whether a value is not within a range of values值是否不在值的范围内
NOT IN()Whether a value is not within a set of values值是否不在一组值内
NOT LIKENegation of simple pattern matching简单模式匹配的否定
STRCMP()Compare two strings比较两个字符串

Comparison operations result in a value of 1 (TRUE), 0 (FALSE), or NULL. 比较操作会导致值1TRUE)、0FALSE)或NULLThese operations work for both numbers and strings. 这些操作适用于数字和字符串。Strings are automatically converted to numbers and numbers to strings as necessary.字符串自动转换为数字,数字根据需要转换为字符串。

The following relational comparison operators can be used to compare not only scalar operands, but row operands:以下关系比较运算符不仅可用于比较标量操作数,还可用于比较行操作数:

=  >  <  >=  <=  <>  !=

The descriptions for those operators later in this section detail how they work with row operands. 本节后面对这些运算符的描述详细介绍了它们如何处理行操作数。For additional examples of row comparisons in the context of row subqueries, see Section 13.2.11.5, “Row Subqueries”.有关行子查询上下文中行比较的其他示例,请参阅第13.2.11.5节,“行子查询”

Some of the functions in this section return values other than 1 (TRUE), 0 (FALSE), or NULL. 本节中的一些函数返回除1TRUE)、0FALSE)或NULL以外的值。LEAST() and GREATEST() are examples of such functions; Section 12.3, “Type Conversion in Expression Evaluation”, describes the rules for comparison operations performed by these and similar functions for determining their return values.LEAST()GREATEST()是此类函数的示例;第12.3节,“表达式求值中的类型转换”描述了这些函数和类似函数为确定其返回值而执行的比较操作的规则。

Note注意

In previous versions of MySQL, when evaluating an expression containing LEAST() or GREATEST(), the server attempted to guess the context in which the function was used, and to coerce the function's arguments to the data type of the expression as a whole. 在MySQL的早期版本中,在计算包含LEAST()GREATEST()的表达式时,服务器试图猜测函数使用的上下文,并将函数的参数强制为整个表达式的数据类型。For example, the arguments to LEAST("11", "45", "2") are evaluated and sorted as strings, so that this expression returns "11". 例如,对LEAST("11", "45", "2")的参数进行求值并将其排序为字符串,因此该表达式返回"11"In MySQL 8.0.3 and earlier, when evaluating the expression LEAST("11", "45", "2") + 0, the server converted the arguments to integers (anticipating the addition of integer 0 to the result) before sorting them, thus returning 2.在MySQL8.0.3和更早版本中,当计算表达式LEAST("11", "45", "2") + 0时,服务器在对参数进行排序之前将参数转换为整数(预期整数0与结果相加),从而返回2。

Beginning with MySQL 8.0.4, the server no longer attempts to infer context in this fashion. 从MySQL8.0.4开始,服务器不再试图以这种方式推断上下文。Instead, the function is executed using the arguments as provided, performing data type conversions to one or more of the arguments if and only if they are not all of the same type. 相反,函数是使用提供的参数执行的,当且仅当一个或多个参数不是同一类型时,才对它们执行数据类型转换。Any type coercion mandated by an expression that makes use of the return value is now performed following function execution. 由使用返回值的表达式强制执行的任何类型强制现在都是在函数执行之后执行的。This means that, in MySQl 8.0.4 and later, LEAST("11", "45", "2") + 0 evaluates to "11" + 0 and thus to integer 11. 这意味着,在MySQl 8.0.4及更高版本中,LEAST("11", "45", "2") + 0的计算结果是"11" + 0,因此是整数11。(Bug #83895, Bug #25123839)

To convert a value to a specific type for comparison purposes, you can use the CAST() function. 要将值转换为特定类型以进行比较,可以使用CAST()函数。String values can be converted to a different character set using CONVERT(). 可以使用CONVERT()将字符串值转换为其他字符集。See Section 12.11, “Cast Functions and Operators”.参见第12.11节,“强制转换函数和运算符”

By default, string comparisons are not case-sensitive and use the current character set. 默认情况下,字符串比较不区分大小写,并使用当前字符集。The default is utf8mb4.默认值为utf8mb4