Table 12.4 Comparison Operators比较运算符
> | |
>= | |
< | |
<> , != | |
<= | |
<=> | |
= | |
BETWEEN ... AND ... | |
COALESCE() | |
GREATEST() | |
IN() | |
INTERVAL() | |
IS | |
IS NOT | |
IS NOT NULL | NOT NULL 值测试 |
IS NULL | NULL 值测试 |
ISNULL() | |
LEAST() | |
LIKE | |
NOT BETWEEN ... AND ... | |
NOT IN() | |
NOT LIKE | |
STRCMP() |
Comparison operations result in a value of 比较操作会导致值1
(TRUE
), 0
(FALSE
), or NULL
. 1
(TRUE
)、0
(FALSE
)或NULL
。These 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
. 1
(TRUE
)、0
(FALSE
)或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节,“表达式求值中的类型转换”描述了这些函数和类似函数为确定其返回值而执行的比较操作的规则。
In previous versions of MySQL, when evaluating an expression containing 在MySQL的早期版本中,在计算包含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. 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 在MySQL8.0.3和更早版本中,当计算表达式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.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, 这意味着,在MySQl 8.0.4及更高版本中,LEAST("11", "45", "2") + 0
evaluates to "11" + 0
and thus to integer 11. 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
。
Equal:等于:
mysql>SELECT 1 = 0;
-> 0 mysql>SELECT '0' = 0;
-> 1 mysql>SELECT '0.0' = 0;
-> 1 mysql>SELECT '0.01' = 0;
-> 0 mysql>SELECT '.01' = 0.01;
-> 1
For row comparisons, 对于行比较,(a, b) = (x, y)
is equivalent to:(a, b) = (x, y)
相当于:
(a = x) AND (b = y)
NULL
-safe equal. NULL
安全等于。This operator performs an equality comparison like the 此运算符执行与=
operator, but returns 1
rather than NULL
if both operands are NULL
, and 0
rather than NULL
if one operand is NULL
.=
运算符类似的相等比较,但如果两个操作数都为NULL
,则返回1
而不是NULL
;如果一个操作数为NULL
,则返回0
而不是NULL
。
The <=>
operator is equivalent to the standard SQL IS NOT DISTINCT FROM
operator.<=>
运算符等价于标准SQL的IS NOT DISTINCT FROM
运算符。
mysql>SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL;
-> 1, 1, 0 mysql>SELECT 1 = 1, NULL = NULL, 1 = NULL;
-> 1, NULL, NULL
For row comparisons, 对于行比较,(a, b) <=> (x, y)
is equivalent to:(a, b) <=> (x, y)
相当于:
(a <=> x) AND (b <=> y)
Not equal:不等于:
mysql>SELECT '.01' <> '0.01';
-> 1 mysql>SELECT .01 <> '0.01';
-> 0 mysql>SELECT 'zapp' <> 'zappp';
-> 1
For row comparisons, 对于行比较,(a, b) <> (x, y)
and (a, b) != (x, y)
are equivalent to:(a, b) <> (x, y)
和(a, b) != (x, y)
相当于:
(a <> x) OR (b <> y)
Less than or equal:小于或等于:
mysql> SELECT 0.1 <= 2;
-> 1
For row comparisons, 对于行比较,(a, b) <= (x, y)
is equivalent to:(a, b) <= (x, y)
相当于:
(a < x) OR ((a = x) AND (b <= y))
Less than:小于:
mysql> SELECT 2 < 2;
-> 0
For row comparisons, 对于行比较,(a, b) < (x, y)
is equivalent to:(a, b) < (x, y)
相当于:
(a < x) OR ((a = x) AND (b < y))
Greater than or equal:大于或等于:
mysql> SELECT 2 >= 2;
-> 1
For row comparisons, 对于行比较,(a, b) >= (x, y)
is equivalent to:(a, b) >= (x, y)
相当于:
(a > x) OR ((a = x) AND (b >= y))
Greater than:大于:
mysql> SELECT 2 > 2;
-> 0
For row comparisons, 对于行比较,(a, b) > (x, y)
is equivalent to:(a, b) > (x, y)
相当于:
(a > x) OR ((a = x) AND (b > y))
If 如果expr
is greater than or equal to min
and expr
is less than or equal to max
, BETWEEN
returns 1
, otherwise it returns 0
. expr
大于或等于min
,expr
小于或等于max
,则BETWEEN
返回1
,否则返回0
。This is equivalent to the expression 如果所有参数的类型相同,这相当于表达式(
if all the arguments are of the same type. min
<= expr
AND expr
<= max
)(
。min
<= expr
AND expr
<= max
)Otherwise type conversion takes place according to the rules described in Section 12.3, “Type Conversion in Expression Evaluation”, but applied to all the three arguments.否则,类型转换将根据第12.3节,“表达式求值中的类型转换”中描述的规则进行,但适用于所有三个参数。
mysql>SELECT 2 BETWEEN 1 AND 3, 2 BETWEEN 3 and 1;
-> 1, 0 mysql>SELECT 1 BETWEEN 2 AND 3;
-> 0 mysql>SELECT 'b' BETWEEN 'a' AND 'c';
-> 1 mysql>SELECT 2 BETWEEN 2 AND '3';
-> 1 mysql>SELECT 2 BETWEEN 2 AND 'x-3';
-> 0
For best results when using 为了在将BETWEEN
with date or time values, use CAST()
to explicitly convert the values to the desired data type. BETWEEN
与日期或时间值一起使用时获得最佳结果,请使用CAST()
将值显式转换为所需的数据类型。Examples: If you compare a 示例:如果将一个DATETIME
to two DATE
values, convert the DATE
values to DATETIME
values. DATETIME
与两个DATE
值进行比较,请将DATE
值转换为DATETIME
值。If you use a string constant such as 如果使用字符串常量(如'2001-1-1'
in a comparison to a DATE
, cast the string to a DATE
.'2001-1-1'
)比较DATE
,请将字符串转换为DATE
。
This is the same as 这与NOT (
.expr
BETWEEN min
AND max
)NOT (
相同。expr
BETWEEN min
AND max
)
Returns the first non-返回列表中的第一个非NULL
value in the list, or NULL
if there are no non-NULL
values.NULL
值,如果没有非NULL
值,则返回NULL
。
The return type of COALESCE()
is the aggregated type of the argument types.COALESCE()
的返回类型是参数类型的聚合类型。
mysql>SELECT COALESCE(NULL,1);
-> 1 mysql>SELECT COALESCE(NULL,NULL,NULL);
-> NULL
With two or more arguments, returns the largest (maximum-valued) argument. 对于两个或多个参数,返回最大(最大值)参数。The arguments are compared using the same rules as for 使用与LEAST()
.LEAST()
相同的规则比较参数。
mysql>SELECT GREATEST(2,0);
-> 2 mysql>SELECT GREATEST(34.0,3.0,5.0,767.0);
-> 767.0 mysql>SELECT GREATEST('B','A','C');
-> 'C'
如果任一个参数为GREATEST()
returns NULL
if any argument is NULL
.NULL
,则GREATEST()
返回NULL
。
Returns 如果1
(true) if expr
is equal to any of the values in the IN()
list, else returns 0
(false).expr
等于IN()
列表中的任一个值,则返回1
(true
),否则返回0
(false
)。
Type conversion takes place according to the rules described in Section 12.3, “Type Conversion in Expression Evaluation”, applied to all the arguments. 类型转换根据第12.3节,“表达式求值中的类型转换”中描述的规则进行,该规则适用于所有参数。If no type conversion is needed for the values in the 如果IN()
list, they are all non-JSON
constants of the same type, and expr
can be compared to each of them as a value of the same type (possibly after type conversion), an optimization takes place. IN()
列表中的值不需要类型转换,那么它们都是同一类型的非JSON
常量,并且expr
可以作为同一类型的值(可能在类型转换之后)与它们中的每一个进行比较,则会进行优化。The values the list are sorted and the search for 对列表中的值进行排序,并使用二进制搜索来搜索expr
is done using a binary search, which makes the IN()
operation very quick.expr
,这使得IN()
操作非常快速。
mysql>SELECT 2 IN (0,3,5,7);
-> 0 mysql>SELECT 'wefwf' IN ('wee','wefwf','weg');
-> 1
IN()
can be used to compare row constructors:IN()
可用于比较行构造函数:
mysql>SELECT (3,4) IN ((1,2), (3,4));
-> 1 mysql>SELECT (3,4) IN ((1,2), (3,5));
-> 0
You should never mix quoted and unquoted values in an 不能在IN()
list because the comparison rules for quoted values (such as strings) and unquoted values (such as numbers) differ. IN()
列表中混合使用带引号和不带引号的值,因为带引号的值(如字符串)和不带引号的值(如数字)的比较规则不同。Mixing types may therefore lead to inconsistent results. 因此,混合类型可能导致不一致的结果。For example, do not write an 例如,不要这样编写IN()
expression like this:IN()
表达式:
SELECT val1 FROM tbl1 WHERE val1 IN (1,2,'a');
Instead, write it like this:需要改成这样:
SELECT val1 FROM tbl1 WHERE val1 IN ('1','2','a');
Implicit type conversion may produce nonintuitive results:隐式类型转换可能会产生非直观的结果:
mysql> SELECT 'a' IN (0), 0 IN ('b');
-> 1, 1
In both cases, the comparison values are converted to floating-point values, yielding 0.0 in each case, and a comparison result of 1 (true).在这两种情况下,比较值都转换为浮点值,每种情况下的结果为0.0,比较结果为1
(true
)。
The number of values in the IN()
list is only limited by the max_allowed_packet
value.IN()
列表中的值的数目仅受max_allowed_packet
值的限制。
To comply with the SQL standard, 为了符合SQL标准,IN()
returns NULL
not only if the expression on the left hand side is NULL
, but also if no match is found in the list and one of the expressions in the list is NULL
.IN()
不仅在左侧的表达式为NULL
时返回NULL
,而且在列表中找不到匹配项并且列表中的一个表达式为NULL
时也返回NULL
。
IN()
syntax can also be used to write certain types of subqueries. IN()
语法也可用于编写某些类型的子查询。See Section 13.2.11.3, “Subqueries with ANY, IN, or SOME”.请参阅第13.2.11.3节,“带有ANY、IN或SOME的子查询”。
This is the same as 这等同于NOT (
.expr
IN (value
,...))NOT (
。expr
IN (value
,...))
Returns 如果0
if N
< N1
, 1
if N
< N2
and so on or -1
if N
is NULL
. N
< N1
则返回0
;如果
则返回N
< N2
1
,依此类推,如果N
为NULL
则返回-1
。All arguments are treated as integers. 所有参数都被视为整数。It is required that 为了使此功能正常工作,需要N1
< N2
< N3
< ...
< Nn
for this function to work correctly. N1
< N2
< N3
< ...
< Nn
。This is because a binary search is used (very fast).这是因为使用了二进制搜索(非常快)。
mysql>SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200);
-> 3 mysql>SELECT INTERVAL(10, 1, 10, 100, 1000);
-> 2 mysql>SELECT INTERVAL(22, 23, 30, 44, 200);
-> 0
Tests a value against a boolean value, where 根据布尔值测试值,其中boolean_value
can be TRUE
, FALSE
, or UNKNOWN
.boolean_value
可以是TRUE
、FALSE
或UNKNOWN
。
mysql> SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN;
-> 1, 1, 1
Tests a value against a boolean value, where 根据布尔值测试值,其中boolean_value
can be TRUE
, FALSE
, or UNKNOWN
.boolean_value
可以是TRUE
、FLASE
或UNKNOWN
。
mysql> SELECT 1 IS NOT UNKNOWN, 0 IS NOT UNKNOWN, NULL IS NOT UNKNOWN;
-> 1, 1, 0
Tests whether a value is 测试值是否为NULL
.NULL
。
mysql> SELECT 1 IS NULL, 0 IS NULL, NULL IS NULL;
-> 0, 0, 1
To work well with ODBC programs, MySQL supports the following extra features when using 为了更好地使用ODBC程序,MySQL在使用IS NULL
:IS NULL
时支持以下额外功能:
If 如果sql_auto_is _null变量设置为1,则在成功插入自动生成的自动增量值的语句后,可以通过发出以下形式的语句来查找该值:sql_auto_is_null
variable is set to 1, then after a statement that successfully inserts an automatically generated AUTO_INCREMENT
value, you can find that value by issuing a statement of the following form:
SELECT * FROMtbl_name
WHEREauto_col
IS NULL
If the statement returns a row, the value returned is the same as if you invoked the 如果语句返回一行,则返回的值与调用LAST_INSERT_ID()
function. LAST_INSERT_ID()
函数时的值相同。For details, including the return value after a multiple-row insert, see Section 12.16, “Information Functions”. 有关详细信息,包括多行插入后的返回值,请参阅第12.16节,“信息函数”。If no 如果没有成功插入AUTO_INCREMENT
value was successfully inserted, the SELECT
statement returns no row.AUTO_INCREMENT
值,SELECT
语句将不返回任何一行。
The behavior of retrieving an 通过使用AUTO_INCREMENT
value by using an IS NULL
comparison can be disabled by setting sql_auto_is_null = 0
. IS NULL
比较来检索AUTO_INCREMENT
值的行为可以通过设置sql_auto_is_null = 0
来禁用。See Section 5.1.8, “Server System Variables”.请参阅第5.1.8节,“服务器系统变量”。
The default value of sql_auto_is_null
is 0.sql_auto_is_null
的默认值为0。
For 对于声明为DATE
and DATETIME
columns that are declared as NOT NULL
, you can find the special date '0000-00-00'
by using a statement like this:NOT NULL
的DATE
和DATETIME
列,可以使用以下语句查找特殊日期'0000-00-00'
:
SELECT * FROMtbl_name
WHEREdate_column
IS NULL
This is needed to get some ODBC applications to work because ODBC does not support a 因为ODBC不支持'0000-00-00'
date value.'0000-00-00'
日期值,所以需要这样做才能使某些ODBC应用程序正常工作。
See Obtaining Auto-Increment Values, and the description for the 请参阅获取自动增量值,以及连接器/ODBC连接参数处的FLAG_AUTO_IS_NULL
option at Connector/ODBC Connection Parameters.FLAG_AUTO_IS_NULL
选项的说明。
Tests whether a value is not 测试值是否为NULL
.NULL
。
mysql> SELECT 1 IS NOT NULL, 0 IS NOT NULL, NULL IS NOT NULL;
-> 1, 1, 0
If 如果expr
is NULL
, ISNULL()
returns 1
, otherwise it returns 0
.expr
为NULL
,则ISNULL()
返回1
,否则返回0
。
mysql>SELECT ISNULL(1+1);
-> 0 mysql>SELECT ISNULL(1/0);
-> 1
可以使用ISNULL()
can be used instead of =
to test whether a value is NULL
. ISNULL()
代替=
来测试值是否为NULL
。(Comparing a value to (将值与NULL
using =
always yields NULL
.)NULL
进行比较时使用=
始终会产生NULL
。)
The ISNULL()
function shares some special behaviors with the IS NULL
comparison operator. ISNULL()
函数与IS NULL
比较运算符共享一些特殊行为。See the description of 请参阅IS NULL
.IS NULL
的说明。
With two or more arguments, returns the smallest (minimum-valued) argument. 对于两个或多个参数,返回最小(最小值)参数。The arguments are compared using the following rules:使用以下规则比较参数:
If any argument is 如果任一个参数为NULL
, the result is NULL
. NULL
,则结果为NULL
。No comparison is needed.不需要比较。
If all arguments are integer-valued, they are compared as integers.如果所有参数都是整数值,则将它们作为整数进行比较。
If at least one argument is double precision, they are compared as double-precision values. 如果至少有一个参数是双精度的,则将它们作为双精度值进行比较。Otherwise, if at least one argument is a 否则,如果至少有一个参数是DECIMAL
value, they are compared as DECIMAL
values.DECIMAL
值,则将它们作为DECIMAL
值进行比较。
If the arguments comprise a mix of numbers and strings, they are compared as strings.如果参数由数字和字符串组成,则将它们作为字符串进行比较。
If any argument is a nonbinary (character) string, the arguments are compared as nonbinary strings.如果任何参数是非二进制(字符)字符串,则这些参数将作为非二进制字符串进行比较。
In all other cases, the arguments are compared as binary strings.在所有其他情况下,参数作为二进制字符串进行比较。
The return type of LEAST()
is the aggregated type of the comparison argument types.LEAST()
的返回类型是比较参数类型的聚合类型。
mysql>SELECT LEAST(2,0);
-> 0 mysql>SELECT LEAST(34.0,3.0,5.0,767.0);
-> 3.0 mysql>SELECT LEAST('B','A','C');
-> 'A'