Table 12.29 Miscellaneous Functions其他函数
ANY_VALUE() | |
BIN_TO_UUID() | |
DEFAULT() | |
GROUPING() | |
INET_ATON() | |
INET_NTOA() | |
INET6_ATON() | |
INET6_NTOA() | |
IS_IPV4() | |
IS_IPV4_COMPAT() | |
IS_IPV4_MAPPED() | |
IS_IPV6() | |
IS_UUID() | |
MASTER_POS_WAIT() | |
NAME_CONST() | |
SLEEP() | |
UUID() | |
UUID_SHORT() | |
UUID_TO_BIN() | |
VALUES() |
This function is useful for 当启用了GROUP BY
queries when the ONLY_FULL_GROUP_BY
SQL mode is enabled, for cases when MySQL rejects a query that you know is valid for reasons that MySQL cannot determine. ONLY_FULL_GROUP_BY
SQL模式时,此函数对于GROUP BY
查询非常有用,当MySQL拒绝一个您知道是有效的查询时,它的原因MySQL无法确定。The function return value and type are the same as the return value and type of its argument, but the function result is not checked for the 函数返回值和类型与其参数的返回值和类型相同,但对于ONLY_FULL_GROUP_BY
SQL mode.ONLY_FULL_GROUP_BY
SQL模式,不会检查函数结果。
For example, if 例如,如果name
is a nonindexed column, the following query fails with ONLY_FULL_GROUP_BY
enabled:name
是非索引列,并且启用了ONLY_FULL_GROUP_BY
,则以下查询将失败:
mysql> SELECT name, address, MAX(age) FROM t GROUP BY name;
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP
BY clause and contains nonaggregated column 'mydb.t.address' which
is not functionally dependent on columns in GROUP BY clause; this
is incompatible with sql_mode=only_full_group_by
The failure occurs because 失败的原因是address
is a nonaggregated column that is neither named among GROUP BY
columns nor functionally dependent on them. address
是一个未聚合的列,它既不在GROUP BY
列中命名,也不在功能上依赖于GROUP BY
列。As a result, the 因此,每个address
value for rows within each name
group is nondeterministic. name
组中的行的地址值是不确定的。There are multiple ways to cause MySQL to accept the query:有多种方法可以使MySQL接受查询:
Alter the table to make 更改表以使name
a primary key or a unique NOT NULL
column. name
成为主键或唯一的NOT NULL
列。This enables MySQL to determine that 这使得MySQL能够确定address
is functionally dependent on name
; that is, address
is uniquely determined by name
. address
在功能上依赖于名称;也就是说,address
由name
唯一地确定。(This technique is inapplicable if (如果必须允许NULL
must be permitted as a valid name
value.)NULL
作为有效的name
值,则此技术不适用。)
Use 使用ANY_VALUE()
to refer to address
:ANY_VALUE()
引用address
:
SELECT name, ANY_VALUE(address), MAX(age) FROM t GROUP BY name;
In this case, MySQL ignores the nondeterminism of 在这种情况下,MySQL会忽略每个名称组中address
values within each name
group and accepts the query. address
值的不确定性,并接受查询。This may be useful if you simply do not care which value of a nonaggregated column is chosen for each group. 如果您只是不关心为每个组选择非聚集列的哪个值,那么这可能很有用。与ANY_VALUE()
is not an aggregate function, unlike functions such as SUM()
or COUNT()
. SUM()
或COUNT()
等函数不同,ANY_VALUE()
不是聚合函数。It simply acts to suppress the test for nondeterminism.它只是用来抑制不确定性测试。
Disable 禁用ONLY_FULL_GROUP_BY
. ONLY_FULL_GROUP_BY
。This is equivalent to using 这相当于使用启用了ANY_VALUE()
with ONLY_FULL_GROUP_BY
enabled, as described in the previous item.ONLY_FULL_GROUP_BY
的ANY_VALUE()
,如前一项所述。
如果列之间存在函数依赖关系,但MySQL无法确定它,则ANY_VALUE()
is also useful if functional dependence exists between columns but MySQL cannot determine it. ANY_VALUE()
也很有用。The following query is valid because 下面的查询是有效的,因为age
is functionally dependent on the grouping column age-1
, but MySQL cannot tell that and rejects the query with ONLY_FULL_GROUP_BY
enabled:age
在功能上依赖于分组列age-1
,但是MySQL无法判断这一点,并且拒绝启用了ONLY_FULL_GROUP_BY
的查询:
SELECT age FROM t GROUP BY age-1;
To cause MySQL to accept the query, use 要使MySQL接受查询,请使用ANY_VALUE()
:ANY_VALUE()
:
SELECT ANY_VALUE(age) FROM t GROUP BY age-1;
在没有ANY_VALUE()
can be used for queries that refer to aggregate functions in the absence of a GROUP BY
clause:GROUP BY
子句的情况下,ANY_VALUE()
都可以用于引用聚合函数的查询:
mysql> SELECT name, MAX(age) FROM t;
ERROR 1140 (42000): In aggregated query without GROUP BY, expression
#1 of SELECT list contains nonaggregated column 'mydb.t.name'; this
is incompatible with sql_mode=only_full_group_by
Without 没有GROUP BY
, there is a single group and it is nondeterministic which name
value to choose for the group. GROUP BY
,就只有一个组,为组选择哪个名称值是不确定的。ANY_VALUE()
tells MySQL to accept the query:ANY_VALUE()
告诉MySQL接受查询:
SELECT ANY_VALUE(name), MAX(age) FROM t;
It may be that, due to some property of a given data set, you know that a selected nonaggregated column is effectively functionally dependent on a 可能是因为,由于给定数据集的某些属性,您知道选定的非聚集列在功能上实际上依赖于GROUP BY
column. GROUP BY
列。For example, an application may enforce uniqueness of one column with respect to another. 例如,应用程序可以强制一列相对于另一列的唯一性。In this case, using 在这种情况下,对有效的函数依赖列使用ANY_VALUE()
for the effectively functionally dependent column may make sense.ANY_VALUE()
可能是有意义的。
For additional discussion, see Section 12.20.3, “MySQL Handling of GROUP BY”.有关更多讨论,请参阅第12.20.3节,“MySQL处理分组方式”。
BIN_TO_UUID(
, binary_uuid
)BIN_TO_UUID(
binary_uuid
, swap_flag
)
BIN_TO_UUID()
is the inverse of UUID_TO_BIN()
. BIN_TO_UUID()
是UUID_TO_BIN()
的倒数。It converts a binary UUID to a string UUID and returns the result. 它将二进制UUID转换为字符串UUID并返回结果。The binary value should be a UUID as a 二进制值应该是一个UUID作为VARBINARY(16)
value. VARBINARY(16)
值。The return value is a 返回值是由五个十六进制数组成的utf8
string of five hexadecimal numbers separated by dashes. utf8
字符串,用破折号分隔。(For details about this format, see the (有关此格式的详细信息,请参阅UUID()
function description.) UUID()
函数说明。)If the UUID argument is 如果UUID参数为NULL
, the return value is NULL
. NULL
,则返回值为NULL
。If any argument is invalid, an error occurs.如果任何参数无效,则会发生错误。
BIN_TO_UUID()
takes one or two arguments:BIN_TO_UUID()
接受一个或两个参数:
The one-argument form takes a binary UUID value. 单参数形式采用二进制UUID值。The UUID value is assumed not to have its time-low and time-high parts swapped. 假定UUID值的时间低部分和时间高部分没有交换。The string result is in the same order as the binary argument.字符串结果与二进制参数的顺序相同。
The two-argument form takes a binary UUID value and a swap-flag value:双参数形式采用二进制UUID值和交换标志值:
If 如果swap_flag
is 0, the two-argument form is equivalent to the one-argument form. swap_flag
为0,则双参数形式等效于单参数形式。The string result is in the same order as the binary argument.字符串结果与二进制参数的顺序相同。
If 如果swap_flag
is 1, the UUID value is assumed to have its time-low and time-high parts swapped. swap_flag
为1,则假定UUID值的时间低部分和时间高部分已交换。These parts are swapped back to their original position in the result value.这些零件将交换回其在结果值中的原始位置。
For usage examples and information about time-part swapping, see the 有关时间段交换的用法示例和信息,请参阅UUID_TO_BIN()
function description.UUID_TO_BIN()
函数说明。
Returns the default value for a table column. 返回表列的默认值。An error results if the column has no default value.如果列没有默认值,则会产生错误。
The use of 仅允许对具有文字默认值的列使用DEFAULT(
to specify the default value for a named column is permitted only for columns that have a literal default value, not for columns that have an expression default value.col_name
)DEFAULT(
来指定命名列的默认值,而不允许对具有表达式默认值的列使用它。col_name
)
mysql> UPDATE t SET i = DEFAULT(i)+1 WHERE id < 100;
Formats the number 将数字X
to a format like '#,###,###.##'
, rounded to D
decimal places, and returns the result as a string. X
格式化为'#,###,###.##'
等格式,四舍五入到D
位小数,并以字符串形式返回结果。For details, see Section 12.8, “String Functions and Operators”.有关详细信息,请参阅第12.8节,“字符串函数和运算符”。
For 对于包含GROUP BY
queries that include a WITH ROLLUP
modifier, the ROLLUP
operation produces super-aggregate output rows where NULL
represents the set of all values. WITH ROLLUP
修饰符的GROUP BY
查询,ROLLUP
操作生成超级聚合输出行,其中NULL
表示所有值的集合。The GROUPING()
function enables you to distinguish NULL
values for super-aggregate rows from NULL
values in regular grouped rows.GROUPING()
函数让你能够区分超级聚合行的NULL
值和常规分组行的NULL
值。
GROUPING()
is permitted in the select list, HAVING
clause, and (as of MySQL 8.0.12) ORDER BY
clause.SELECT
列表、HAVING
子句和(从MySQL 8.0.12开始)ORDER BY
子句中允许GROUPING()
。
Each argument to GROUPING()
must be an expression that exactly matches an expression in the GROUP BY
clause. GROUPING()
的每个参数必须是与GROUP BY
子句中的表达式完全匹配的表达式。The expression cannot be a positional specifier. 表达式不能是位置说明符。For each expression, 对于每个表达式,如果当前行中的表达式值是表示超级聚合值的GROUPING()
produces 1 if the expression value in the current row is a NULL
representing a super-aggregate value. NULL
,GROUPING()
将生成1。Otherwise, 否则,GROUPING()
produces 0, indicating that the expression value is a NULL
for a regular result row or is not NULL
.GROUPING()
将生成0,表示表达式值对于常规结果行为NULL
或不为NULL
。
Suppose that table 假设表t1
contains these rows, where NULL
indicates something like “other” or “unknown”:t1
包含这些行,其中NULL
表示类似“other”或“unknown”的内容:
mysql> SELECT * FROM t1;
+------+-------+----------+
| name | size | quantity |
+------+-------+----------+
| ball | small | 10 |
| ball | large | 20 |
| ball | NULL | 5 |
| hoop | small | 15 |
| hoop | large | 5 |
| hoop | NULL | 3 |
+------+-------+----------+
A summary of the table without 不带WITH ROLLUP
looks like this:WITH ROLLUP
的表的摘要如下所示:
mysql>SELECT name, size, SUM(quantity) AS quantity
FROM t1
GROUP BY name, size;
+------+-------+----------+ | name | size | quantity | +------+-------+----------+ | ball | small | 10 | | ball | large | 20 | | ball | NULL | 5 | | hoop | small | 15 | | hoop | large | 5 | | hoop | NULL | 3 | +------+-------+----------+
The result contains 结果包含NULL
values, but those do not represent super-aggregate rows because the query does not include WITH ROLLUP
.NULL
值,但这些值不表示超级聚合行,因为查询不包含WITH ROLLUP
。
Adding 添加WITH ROLLUP
produces super-aggregate summary rows containing additional NULL
values. WITH ROLLUP
将生成包含其他NULL
值的超级聚合摘要行。However, without comparing this result to the previous one, it is not easy to see which 但是,如果不将此结果与前一个结果进行比较,就不容易看出哪些NULL
values occur in super-aggregate rows and which occur in regular grouped rows:NULL
值出现在超级聚合行中,哪些NULL
值出现在常规分组行中:
mysql>SELECT name, size, SUM(quantity) AS quantity
FROM t1
GROUP BY name, size WITH ROLLUP;
+------+-------+----------+ | name | size | quantity | +------+-------+----------+ | ball | NULL | 5 | | ball | large | 20 | | ball | small | 10 | | ball | NULL | 35 | | hoop | NULL | 3 | | hoop | large | 5 | | hoop | small | 15 | | hoop | NULL | 23 | | NULL | NULL | 58 | +------+-------+----------+
To distinguish 要区分超级聚合行中的NULL
values in super-aggregate rows from those in regular grouped rows, use GROUPING()
, which returns 1 only for super-aggregate NULL
values:NULL
值和常规分组行中的NULL
值,请使用GROUPING()
,它仅为超级聚合空值返回1:
mysql>SELECT
name, size, SUM(quantity) AS quantity,
GROUPING(name) AS grp_name,
GROUPING(size) AS grp_size
FROM t1
GROUP BY name, size WITH ROLLUP;
+------+-------+----------+----------+----------+ | name | size | quantity | grp_name | grp_size | +------+-------+----------+----------+----------+ | ball | NULL | 5 | 0 | 0 | | ball | large | 20 | 0 | 0 | | ball | small | 10 | 0 | 0 | | ball | NULL | 35 | 0 | 1 | | hoop | NULL | 3 | 0 | 0 | | hoop | large | 5 | 0 | 0 | | hoop | small | 15 | 0 | 0 | | hoop | NULL | 23 | 0 | 1 | | NULL | NULL | 58 | 1 | 1 | +------+-------+----------+----------+----------+
Common uses for GROUPING()
:GROUPING()
的常见用法:
Substitute a label for super-aggregate 用标签替换超级聚合NULL
values:NULL
值:
mysql>SELECT
IF(GROUPING(name) = 1, 'All items', name) AS name,
IF(GROUPING(size) = 1, 'All sizes', size) AS size,
SUM(quantity) AS quantity
FROM t1
GROUP BY name, size WITH ROLLUP;
+-----------+-----------+----------+ | name | size | quantity | +-----------+-----------+----------+ | ball | NULL | 5 | | ball | large | 20 | | ball | small | 10 | | ball | All sizes | 35 | | hoop | NULL | 3 | | hoop | large | 5 | | hoop | small | 15 | | hoop | All sizes | 23 | | All items | All sizes | 58 | +-----------+-----------+----------+
Return only super-aggregate lines by filtering out the regular grouped lines:通过筛选出常规分组行,仅返回超级聚合行:
mysql>SELECT name, size, SUM(quantity) AS quantity
FROM t1
GROUP BY name, size WITH ROLLUP
HAVING GROUPING(name) = 1 OR GROUPING(size) = 1;
+------+------+----------+ | name | size | quantity | +------+------+----------+ | ball | NULL | 35 | | hoop | NULL | 23 | | NULL | NULL | 58 | +------+------+----------+
GROUPING()
permits multiple expression arguments. GROUPING()
允许多个表达式参数。In this case, the 在本例中,GROUPING()
return value represents a bitmask combined from the results for each expression, where the lowest-order bit corresponds to the result for the rightmost expression. GROUPING()
返回值表示从每个表达式的结果组合而来的位掩码,其中最低位对应于最右边表达式的结果。For example, with three expression arguments, 例如,使用三个表达式参数GROUPING(
is evaluated like this:expr1
, expr2
, expr3
)GROUPING(
等同于下面:expr1
, expr2
, expr3
)
result for GROUPING(expr3
) + result for GROUPING(expr2
) << 1 + result for GROUPING(expr1
) << 2
The following query shows how 以下查询显示如何将单个参数的GROUPING()
results for single arguments combine for a multiple-argument call to produce a bitmask value:GROUPING()
结果与多参数调用结合起来以生成位掩码值:
mysql>SELECT
name, size, SUM(quantity) AS quantity,
GROUPING(name) AS grp_name,
GROUPING(size) AS grp_size,
GROUPING(name, size) AS grp_all
FROM t1
GROUP BY name, size WITH ROLLUP;
+------+-------+----------+----------+----------+---------+ | name | size | quantity | grp_name | grp_size | grp_all | +------+-------+----------+----------+----------+---------+ | ball | NULL | 5 | 0 | 0 | 0 | | ball | large | 20 | 0 | 0 | 0 | | ball | small | 10 | 0 | 0 | 0 | | ball | NULL | 35 | 0 | 1 | 1 | | hoop | NULL | 3 | 0 | 0 | 0 | | hoop | large | 5 | 0 | 0 | 0 | | hoop | small | 15 | 0 | 0 | 0 | | hoop | NULL | 23 | 0 | 1 | 1 | | NULL | NULL | 58 | 1 | 1 | 3 | +------+-------+----------+----------+----------+---------+
With multiple expression arguments, the 对于多个表达式参数,如果任何表达式表示超级聚合值,则GROUPING()
return value is nonzero if any expression represents a super-aggregate value. GROUPING()
返回值为非零。Multiple-argument 因此,多参数的GROUPING()
syntax thus provides a simpler way to write the earlier query that returned only super-aggregate rows, by using a single multiple-argument GROUPING()
call rather than multiple single-argument calls:GROUPING()
语法通过使用单个多参数的GROUPING()
调用而不是多个单参数调用,提供了一种更简单的方法来编写只返回超级聚合行的早期查询:
mysql>SELECT name, size, SUM(quantity) AS quantity
FROM t1
GROUP BY name, size WITH ROLLUP
HAVING GROUPING(name, size) <> 0;
+------+------+----------+ | name | size | quantity | +------+------+----------+ | ball | NULL | 35 | | hoop | NULL | 23 | | NULL | NULL | 58 | +------+------+----------+
Use of GROUPING()
is subject to these limitations:GROUPING()
的使用受以下限制:
Do not use subquery 不要将子查询GROUP BY
expressions as GROUPING()
arguments because matching might fail. GROUP BY
表达式用作GROUPING()
参数,因为匹配可能会失败。For example, matching fails for this query:例如,匹配此查询失败:
mysql>SELECT GROUPING((SELECT MAX(name) FROM t1))
FROM t1
GROUP BY (SELECT MAX(name) FROM t1) WITH ROLLUP;
ERROR 3580 (HY000): Argument #1 of GROUPING function is not in GROUP BY
GROUP BY
literal expressions should not be used within a HAVING
clause as GROUPING()
arguments. GROUP BY
文字表达式不应在HAVING
子句中用作GROUPING()
参数。Due to differences between when the optimizer evaluates 由于优化器计算GROUP BY
and HAVING
, matching may succeed but GROUPING()
evaluation does not produce the expected result. GROUP BY
和HAVING
时的不同,匹配可能会成功,但GROUPING()
计算不会产生预期的结果。Consider this query:考虑这个查询:
SELECT a AS f1, 'w' AS f2 FROM t GROUP BY f1, f2 WITH ROLLUP HAVING GROUPING(f2) = 1;
GROUPING()
is evaluated earlier for the literal constant expression than for the HAVING
clause as a whole and returns 0. GROUPING()
的计算时间早于HAVING
子句的计算时间,返回0。To check whether a query such as this is affected, use 要检查这样的查询是否受到影响,请使用EXPLAIN
and look for Impossible having
in the Extra
column.EXPLAIN
并在额外的列中查找Impossible having
。
For more information about 有关WITH ROLLUP
and GROUPING()
, see Section 12.20.2, “GROUP BY Modifiers”.WITH ROLLUP
和GROUPING()
的详细信息,请参阅第12.20.2节,“按修饰符分组”。
Given the dotted-quad representation of an IPv4 network address as a string, returns an integer that represents the numeric value of the address in network byte order (big endian). 给定IPv4网络地址的虚线四元表示为字符串,返回一个整数,该整数以网络字节顺序(大端)表示地址的数值。如果INET_ATON()
returns NULL
if it does not understand its argument.INET_ATON()
不理解其参数,则返回NULL
。
mysql> SELECT INET_ATON('10.0.5.9');
-> 167773449
For this example, the return value is calculated as 10×2563 + 0×2562 + 5×256 + 9.对于本例,返回值计算为10×2563 + 0×2562 + 5×256 + 9。
INET_ATON()
may or may not return a non-NULL
result for short-form IP addresses (such as '127.1'
as a representation of '127.0.0.1'
). INET_ATON()
可能返回也可能不返回短格式IP地址的非空结果(例如表示'127.0.0.1'
的'127.1'
)。Because of this, 因此,INET_ATON()
a should not be used for such addresses.INET_ATON()
不应用于此类地址。
To store values generated by 要存储INET_ATON()
, use an INT UNSIGNED
column rather than INT
, which is signed. INET_ATON()
生成的值,请使用INT UNSIGNED
列,而不是INT
,后者是有符号的。If you use a signed column, values corresponding to IP addresses for which the first octet is greater than 127 cannot be stored correctly. 如果使用有符号列,则无法正确存储与第一个八位字节大于127的IP地址对应的值。See Section 11.1.7, “Out-of-Range and Overflow Handling”.请参阅第11.1.7节,“超出范围和溢出处理”。
Given a numeric IPv4 network address in network byte order, returns the dotted-quad string representation of the address as a string in the connection character set. 给定按网络字节顺序排列的数字IPv4网络地址,以连接字符集中的字符串形式返回该地址的虚线四元字符串表示形式。如果INET_NTOA()
returns NULL
if it does not understand its argument.INET_NTOA()
不理解其参数,则返回NULL
。
mysql> SELECT INET_NTOA(167773449);
-> '10.0.5.9'
Given an IPv6 or IPv4 network address as a string, returns a binary string that represents the numeric value of the address in network byte order (big endian). 给定一个IPv6或IPv4网络地址作为字符串,返回一个二进制字符串,该字符串以网络字节顺序(大端)表示地址的数值。Because numeric-format IPv6 addresses require more bytes than the largest integer type, the representation returned by this function has the 由于数字格式IPv6地址需要的字节数比最大整数类型多,因此此函数返回的表示形式具有VARBINARY
data type: VARBINARY(16)
for IPv6 addresses and VARBINARY(4)
for IPv4 addresses. VARBINARY
数据类型:VARBINARY(16)
表示IPv6地址,VARBINARY(4)
表示IPv4地址。If the argument is not a valid address, 如果参数不是有效地址,INET6_ATON()
returns NULL
.INET6_ATON()
将返回NULL
。
The following examples use 以下示例使用HEX()
to display the INET6_ATON()
result in printable form:HEX()
以可打印形式显示INET6_ATON()
结果:
mysql>SELECT HEX(INET6_ATON('fdfe::5a55:caff:fefa:9089'));
-> 'FDFE0000000000005A55CAFFFEFA9089' mysql>SELECT HEX(INET6_ATON('10.0.5.9'));
-> '0A000509'
INET6_ATON()
observes several constraints on valid arguments. INET6_ATON()
观察有效参数的几个约束。These are given in the following list along with examples.下面列出了这些示例。
A trailing zone ID is not permitted, as in 不允许使用尾部区域ID,如fe80::3%1
or fe80::3%eth0
.fe80::3%1
或fe80::3%0
。
A trailing network mask is not permitted, as in 不允许使用尾部网络掩码,如2001:45f:3:ba::/64
or 198.51.100.0/24
.2001:45f:3:ba::/64
或198.51.100.0/24
。
For values representing IPv4 addresses, only classless addresses are supported. 对于表示IPv4地址的值,只支持无类地址。Classful addresses such as 拒绝类地址,如198.51.1
are rejected. A trailing port number is not permitted, as in 198.51.100.2:8080
. 198.51.1
。不允许使用尾随端口号,如198.51.100.2:8080
。Hexadecimal numbers in address components are not permitted, as in 地址组件中不允许使用十六进制数,如198.0xa0.1.2
. 198.0xa0.1.2
。Octal numbers are not supported: 不支持八进制数:198.51.010.1
is treated as 198.51.10.1
, not 198.51.8.1
. 198.51.010.1
被视为198.51.10.1
,而不是198.51.8.1
。These IPv4 constraints also apply to IPv6 addresses that have IPv4 address parts, such as IPv4-compatible or IPv4-mapped addresses.这些IPv4约束也适用于具有IPv4地址部分的IPv6地址,例如IPv4兼容地址或IPv4映射地址。
To convert an IPv4 address 要将以数值形式表示为expr
represented in numeric form as an INT
value to an IPv6 address represented in numeric form as a VARBINARY
value, use this expression:INT
值的IPv4地址expr
转换为以数值形式表示为VARBINARY
值的IPv6地址,请使用以下表达式:
INET6_ATON(INET_NTOA(expr
))
For example:例如:
mysql> SELECT HEX(INET6_ATON(INET_NTOA(167773449)));
-> '0A000509'
If 如果从mysql客户机中调用INET6_ATON()
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex
. INET6_ATON()
,则二进制字符串将使用十六进制表示法显示,具体取决于--binary-as-hex的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql-mysql命令行客户端”。
Given an IPv6 or IPv4 network address represented in numeric form as a binary string, returns the string representation of the address as a string in the connection character set. 给定以数字形式表示为二进制字符串的IPv6或IPv4网络地址,将该地址的字符串表示形式作为连接字符集中的字符串返回。If the argument is not a valid address, 如果参数不是有效地址,INET6_NTOA()
returns NULL
.INET6_NTOA()
将返回NULL
。
INET6_NTOA()
has these properties:INET6_NTOA()
具有以下属性:
It does not use operating system functions to perform conversions, thus the output string is platform independent.它不使用操作系统函数来执行转换,因此输出字符串与平台无关。
The return string has a maximum length of 39 (4 x 8 + 7). 返回字符串的最大长度为39(4 x 8+7)。Given this statement:鉴于此声明:
CREATE TABLE t AS SELECT INET6_NTOA(expr
) AS c1;
The resulting table would have this definition:生成的表将具有以下定义:
CREATE TABLE t (c1 VARCHAR(39) CHARACTER SET utf8 DEFAULT NULL);
The return string uses lowercase letters for IPv6 addresses.返回字符串使用小写字母表示IPv6地址。
mysql>SELECT INET6_NTOA(INET6_ATON('fdfe::5a55:caff:fefa:9089'));
-> 'fdfe::5a55:caff:fefa:9089' mysql>SELECT INET6_NTOA(INET6_ATON('10.0.5.9'));
-> '10.0.5.9' mysql>SELECT INET6_NTOA(UNHEX('FDFE0000000000005A55CAFFFEFA9089'));
-> 'fdfe::5a55:caff:fefa:9089' mysql>SELECT INET6_NTOA(UNHEX('0A000509'));
-> '10.0.5.9'
If 如果从mysql客户机中调用INET6_NTOA()
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex
. INET6_NTOA()
,则二进制字符串将使用十六进制表示法显示,具体取决于--binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql-mysql命令行客户端”。
Returns 1 if the argument is a valid IPv4 address specified as a string, 0 otherwise.如果参数是指定为字符串的有效IPv4地址,则返回1,否则返回0。
mysql> SELECT IS_IPV4('10.0.5.9'), IS_IPV4('10.0.5.256');
-> 1, 0
For a given argument, if 对于给定的参数,如果IS_IPV4()
returns 1, INET_ATON()
(and INET6_ATON()
) returns non-NULL
. IS_IPV4()
返回1,INET_ATON()
(并且INET6_ATON()
)返回非NULL
。The converse statement is not true: In some cases, 相反的语句不是真的:在某些情况下,当INET_ATON()
returns non-NULL
when IS_IPV4()
returns 0.IS_IPV4()
返回0时,INET_ATON()
返回非NULL
。
As implied by the preceding remarks, 正如前面的说明所暗示的,IS_IPV4()
is more strict than INET_ATON()
about what constitutes a valid IPv4 address, so it may be useful for applications that need to perform strong checks against invalid values. IS_IPV4()
比INET_ATON()
对于什么是有效的IPV4地址更严格,因此对于需要对无效值执行强检查的应用程序来说,它可能很有用。Alternatively, use 或者,使用INET6_ATON()
to convert IPv4 addresses to internal form and check for a NULL
result (which indicates an invalid address). INET6_ATON()
将IPv4地址转换为内部格式,并检查空结果(表示地址无效)。INET6_ATON()
is equally strong as IS_IPV4()
about checking IPv4 addresses.INET6_ATON()
和IPV4()
在检查IPV4地址方面同样强大。
This function takes an IPv6 address represented in numeric form as a binary string, as returned by 此函数采用由INET6_ATON()
. INET6_ATON()
返回的以数字形式表示为二进制字符串的IPv6地址。It returns 1 if the argument is a valid IPv4-compatible IPv6 address, 0 otherwise. 如果参数是有效的IPv4兼容IPv6地址,则返回1,否则返回0。IPv4-compatible addresses have the form IPv4兼容的地址具有如下形式:::
.ipv4_address
::
。ipv4_address
mysql>SELECT IS_IPV4_COMPAT(INET6_ATON('::10.0.5.9'));
-> 1 mysql>SELECT IS_IPV4_COMPAT(INET6_ATON('::ffff:10.0.5.9'));
-> 0
The IPv4 part of an IPv4-compatible address can also be represented using hexadecimal notation. IPv4兼容地址的IPv4部分也可以使用十六进制表示法表示。For example, 例如,198.51.100.1
has this raw hexadecimal value:198.51.100.1
具有以下原始十六进制值:
mysql> SELECT HEX(INET6_ATON('198.51.100.1'));
-> 'C6336401'
Expressed in IPv4-compatible form, 以IPv4兼容形式表示,::198.51.100.1
is equivalent to ::c0a8:0001
or (without leading zeros) ::c0a8:1
::198.51.100.1
等同于::c0a8:0001
或(不带前导零)::c0a8:1
mysql>SELECT
->IS_IPV4_COMPAT(INET6_ATON('::198.51.100.1')),
->IS_IPV4_COMPAT(INET6_ATON('::c0a8:0001')),
->IS_IPV4_COMPAT(INET6_ATON('::c0a8:1'));
-> 1, 1, 1
This function takes an IPv6 address represented in numeric form as a binary string, as returned by 此函数采用由INET6_ATON()
. INET6_ATON()
返回的以数字形式表示为二进制字符串的IPv6地址。It returns 1 if the argument is a valid IPv4-mapped IPv6 address, 0 otherwise. 如果参数是有效的IPv4映射IPv6地址,则返回1,否则返回0。IPv4-mapped addresses have the form IPv4映射地址的格式为::ffff:
.ipv4_address
::ffff:
。ipv4_address
mysql>SELECT IS_IPV4_MAPPED(INET6_ATON('::10.0.5.9'));
-> 0 mysql>SELECT IS_IPV4_MAPPED(INET6_ATON('::ffff:10.0.5.9'));
-> 1
As with 与IS_IPV4_COMPAT()
the IPv4 part of an IPv4-mapped address can also be represented using hexadecimal notation:IS_IPV4_COMPAT()
一样,IPV4映射地址的IPV4部分也可以使用十六进制表示法表示:
mysql>SELECT
->IS_IPV4_MAPPED(INET6_ATON('::ffff:198.51.100.1')),
->IS_IPV4_MAPPED(INET6_ATON('::ffff:c0a8:0001')),
->IS_IPV4_MAPPED(INET6_ATON('::ffff:c0a8:1'));
-> 1, 1, 1
Returns 1 if the argument is a valid IPv6 address specified as a string, 0 otherwise. 如果参数是指定为字符串的有效IPv6地址,则返回1,否则返回0。This function does not consider IPv4 addresses to be valid IPv6 addresses.这个函数不考虑IPv4地址是有效的IPv6地址。
mysql> SELECT IS_IPV6('10.0.5.9'), IS_IPV6('::1');
-> 0, 1
For a given argument, if 对于给定参数,如果IS_IPV6()
returns 1, INET6_ATON()
returns non-NULL
.IS_IPV6()
返回1,则INET6_ATON()
返回非NULL
。
Returns 1 if the argument is a valid string-format UUID, 0 if the argument is not a valid UUID, and 如果参数是有效的字符串格式UUID,则返回1;如果参数不是有效的UUID,则返回0;如果参数为NULL
if the argument is NULL
.NULL
,则返回NULL
。
“Valid” means that the value is in a format that can be parsed. “Valid”表示值的格式可以解析。That is, it has the correct length and contains only the permitted characters (hexadecimal digits in any lettercase and, optionally, dashes and curly braces). 也就是说,它具有正确的长度,并且只包含允许的字符(任何字母大小写中的十六进制数字,以及可选的破折号和大括号)。This format is most common:这种格式最常见:
aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
These other formats are also permitted:这些其他格式也是允许的:
aaaaaaaabbbbccccddddeeeeeeeeeeee {aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee}
For the meanings of fields within the value, see the 有关值中字段的含义,请参见UUID()
function description.UUID()
函数说明。
mysql>SELECT IS_UUID('6ccd780c-baba-1026-9564-5b8c656024db');
+-------------------------------------------------+ | IS_UUID('6ccd780c-baba-1026-9564-5b8c656024db') | +-------------------------------------------------+ | 1 | +-------------------------------------------------+ mysql>SELECT IS_UUID('6CCD780C-BABA-1026-9564-5B8C656024DB');
+-------------------------------------------------+ | IS_UUID('6CCD780C-BABA-1026-9564-5B8C656024DB') | +-------------------------------------------------+ | 1 | +-------------------------------------------------+ mysql>SELECT IS_UUID('6ccd780cbaba102695645b8c656024db');
+---------------------------------------------+ | IS_UUID('6ccd780cbaba102695645b8c656024db') | +---------------------------------------------+ | 1 | +---------------------------------------------+ mysql>SELECT IS_UUID('{6ccd780c-baba-1026-9564-5b8c656024db}');
+---------------------------------------------------+ | IS_UUID('{6ccd780c-baba-1026-9564-5b8c656024db}') | +---------------------------------------------------+ | 1 | +---------------------------------------------------+ mysql>SELECT IS_UUID('6ccd780c-baba-1026-9564-5b8c6560');
+---------------------------------------------+ | IS_UUID('6ccd780c-baba-1026-9564-5b8c6560') | +---------------------------------------------+ | 0 | +---------------------------------------------+ mysql>SELECT IS_UUID(RAND());
+-----------------+ | IS_UUID(RAND()) | +-----------------+ | 0 | +-----------------+
MASTER_POS_WAIT(
log_name
,log_pos
[,timeout
][,channel
])
This function is useful for control of source/replica synchronization. 此函数用于控制源/副本同步。It blocks until the replica has read and applied all updates up to the specified position in the source's binary log. 它将阻塞,直到复制副本读取并应用所有更新到源二进制日志中的指定位置为止。The return value is the number of log events the replica had to wait for to advance to the specified position. 返回值是复制副本必须等待才能前进到指定位置的日志事件数。The function returns 如果复制SQL线程未启动、副本的源信息未初始化、参数不正确或发生错误,则函数返回NULL
if the replication SQL thread is not started, the replica's source information is not initialized, the arguments are incorrect, or an error occurs. NULL
。It returns 如果超过超时时间,则返回-1
if the timeout has been exceeded. -1
。If the replication SQL thread stops while 如果复制SQL线程在MASTER_POS_WAIT()
is waiting, the function returns NULL
. MASTER_POS_WAIT()
等待时停止,则函数返回NULL
。If the replica is past the specified position, the function returns immediately.如果复制副本超过指定位置,则函数将立即返回。
On a multithreaded replica, the function waits until expiry of the limit set by the 在多线程副本上,当调用检查点操作以更新副本的状态时,该函数将一直等待,直到slave_checkpoint_group
or slave_checkpoint_period
system variable, when the checkpoint operation is called to update the status of the replica. slave_checkpoint_group
或slave_checkpoint_period
系统变量设置的限制到期。Depending on the setting for the system variables, the function might therefore return some time after the specified position was reached.根据系统变量的设置,函数可能会在到达指定位置后返回一段时间。
If binary log transaction compression is in use and the transaction payload at the specified position is compressed (as a 如果正在使用二进制日志事务压缩,并且指定位置的事务负载被压缩(作为Transaction_payload_event
), the function waits until the whole transaction has been read and applied, and the positions have updated.Transaction_payload_event
),则函数将等待整个事务被读取和应用,并且位置已更新。
If a 如果指定了timeout
value is specified, MASTER_POS_WAIT()
stops waiting when timeout
seconds have elapsed. timeout
,则当timeout
秒数已过时,MASTER_POS_WAIT()
将停止等待。timeout
must be greater than 0; a zero or negative timeout
means no timeout.timeout
必须大于0;零或负timeout
表示没有超时。
The optional 可选channel
value enables you to name which replication channel the function applies to. channel
值使您能够命名该函数应用于哪个复制通道。See Section 17.2.2, “Replication Channels” for more information.有关更多信息,请参阅第17.2.2节,“复制通道”。
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format
is set to STATEMENT
.binlog_format
设置为STATEMENT
时使用此函数,则会记录警告。
Returns the given value. When used to produce a result set column, 返回给定值。当用于生成结果集列时,NAME_CONST()
causes the column to have the given name. NAME_CONST()
使该列具有给定的名称。The arguments should be constants.参数应该是常量。
mysql> SELECT NAME_CONST('myname', 14);
+--------+
| myname |
+--------+
| 14 |
+--------+
This function is for internal use only. 此功能仅供内部使用。The server uses it when writing statements from stored programs that contain references to local program variables, as described in Section 25.7, “Stored Program Binary Logging”. 服务器在从包含对本地程序变量引用的存储程序中写入语句时使用它,如第25.7节,“存储程序二进制日志记录”所述。You might see this function in the output from mysqlbinlog.您可能会在mysqlbinlog的输出中看到这个函数。
For your applications, you can obtain exactly the same result as in the example just shown by using simple aliasing, like this:对于您的应用程序,您可以通过使用简单的别名获得与刚才所示示例完全相同的结果,如下所示:
mysql> SELECT 14 AS myname;
+--------+
| myname |
+--------+
| 14 |
+--------+
1 row in set (0.00 sec)
See Section 13.2.10, “SELECT Statement”, for more information about column aliases.有关列别名的详细信息,请参阅第13.2.10节,“SELECT语句”。
Sleeps (pauses) for the number of seconds given by the 休眠(暂停)duration
argument, then returns 0. duration
参数给定的秒数,然后返回0。The duration may have a fractional part. 持续时间可以是小数部分。If the argument is 如果参数为NULL
or negative, SLEEP()
produces a warning, or an error in strict SQL mode.NULL
或负值,SLEEP()
将在严格SQL模式下生成警告或错误。
When sleep returns normally (without interruption), it returns 0:当睡眠恢复正常(无中断)时,返回0:
mysql> SELECT SLEEP(1000);
+-------------+
| SLEEP(1000) |
+-------------+
| 0 |
+-------------+
When 当SLEEP()
is the only thing invoked by a query that is interrupted, it returns 1 and the query itself returns no error. SLEEP()
是被中断的查询调用的唯一对象时,它返回1,查询本身不返回错误。This is true whether the query is killed or times out:无论查询是终止还是超时,都是如此:
This statement is interrupted using 使用另一个会话的KILL QUERY
from another session:KILL QUERY
中断此语句:
mysql> SELECT SLEEP(1000);
+-------------+
| SLEEP(1000) |
+-------------+
| 1 |
+-------------+
This statement is interrupted by timing out:此语句被超时中断:
mysql> SELECT /*+ MAX_EXECUTION_TIME(1) */ SLEEP(1000);
+-------------+
| SLEEP(1000) |
+-------------+
| 1 |
+-------------+
When 当SLEEP()
is only part of a query that is interrupted, the query returns an error:SLEEP()
只是被中断的查询的一部分时,查询将返回错误:
This statement is interrupted using 使用另一个会话的KILL QUERY
from another session:KILL QUERY
中断此语句:
mysql> SELECT 1 FROM t1 WHERE SLEEP(1000);
ERROR 1317 (70100): Query execution was interrupted
This statement is interrupted by timing out:此语句被超时中断:
mysql> SELECT /*+ MAX_EXECUTION_TIME(1000) */ 1 FROM t1 WHERE SLEEP(1000);
ERROR 3024 (HY000): Query execution was interrupted, maximum statement
execution time exceeded
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format
is set to STATEMENT
.binlog_format
设置为STATEMENT
时使用此函数,则会记录警告。
Returns a Universal Unique Identifier (UUID) generated according to RFC 4122, “A Universally Unique IDentifier (UUID) URN Namespace” (http://www.ietf.org/rfc/rfc4122.txt).返回根据RFC 4122“通用唯一标识符(UUID)URN命名空间”生成的通用唯一标识符(UUID)(http://www.ietf.org/rfc/rfc4122.txt)。
A UUID is designed as a number that is globally unique in space and time. UUID被设计为在空间和时间上全局唯一的数字。Two calls to 对UUID()
are expected to generate two different values, even if these calls are performed on two separate devices not connected to each other.UUID()
的两个调用预期会生成两个不同的值,即使这些调用是在两个彼此未连接的单独设备上执行的。
Although 虽然UUID()
values are intended to be unique, they are not necessarily unguessable or unpredictable. UUID()
值是唯一的,但它们不一定是不可用或不可预测的。If unpredictability is required, UUID values should be generated some other way.如果需要不可预测性,则应该以其他方式生成UUID值。
UUID()
returns a value that conforms to UUID version 1 as described in RFC 4122. UUID()
返回一个值,该值符合RFC 4122中描述的UUID版本1。The value is a 128-bit number represented as a 该值是一个128位的数字,表示为五个十六进制数字的utf8
string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
format:utf8
字符串,格式为aaaaaaaa-bbbb-cccc-dddd-eeee
:
The first three numbers are generated from the low, middle, and high parts of a timestamp. 前三个数字由时间戳的低、中、高部分生成。The high part also includes the UUID version number.较高的部分还包括UUID版本号。
The fourth number preserves temporal uniqueness in case the timestamp value loses monotonicity (for example, due to daylight saving time).第四个数字保持时间唯一性,以防时间戳值失去单调性(例如,由于夏令时)。
The fifth number is an IEEE 802 node number that provides spatial uniqueness. 第五个数字是提供空间唯一性的ieee802节点号。A random number is substituted if the latter is not available (for example, because the host device has no Ethernet card, or it is unknown how to find the hardware address of an interface on the host operating system). 如果后者不可用(例如,因为主机设备没有以太网卡,或者不知道如何在主机操作系统上找到接口的硬件地址),则替换随机数。In this case, spatial uniqueness cannot be guaranteed. 在这种情况下,无法保证空间唯一性。Nevertheless, a collision should have very low probability.然而,碰撞的概率应该很低。
The MAC address of an interface is taken into account only on FreeBSD, Linux, and Windows. On other operating systems, MySQL uses a randomly generated 48-bit number.只有在FreeBSD、Linux和Windows上才考虑接口的MAC地址。在其他操作系统上,MySQL使用随机生成的48位数字。
mysql> SELECT UUID();
-> '6ccd780c-baba-1026-9564-5b8c656024db'
To convert between string and binary UUID values, use the 要在字符串和二进制UUID值之间转换,请使用UUID_TO_BIN()
and BIN_TO_UUID()
functions. UUID_To_BIN()
和BIN_To_UUID()
函数。To check whether a string is a valid UUID value, use the 要检查字符串是否是有效的UUID值,请使用IS_UUID()
function.IS_UUID()
函数。
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format
is set to STATEMENT
.binlog_format
设置为STATEMENT
时使用此函数,则会记录警告。
Returns a “short” universal identifier as a 64-bit unsigned integer. 以64位无符号整数的形式返回“短”通用标识符。Values returned by UUID_SHORT()
differ from the string-format 128-bit identifiers returned by the UUID()
function and have different uniqueness properties. UUID_SHORT()
返回的值与UUID()
函数返回的字符串格式128位标识符不同,并且具有不同的唯一性属性。The value of 如果满足以下条件,则UUID_SHORT()
is guaranteed to be unique if the following conditions hold:UUID_SHORT()
的值保证是唯一的:
The 当前服务器的server_id
value of the current server is between 0 and 255 and is unique among your set of source and replica serversserver_id
值介于0和255之间,并且在源服务器和副本服务器集中是唯一的
You do not set back the system time for your server host between mysqld restarts在mysqld重新启动之间,不会为服务器主机设置系统时间
You invoke 在mysqld重启之间,平均每秒调用UUID_SHORT()
on average fewer than 16 million times per second between mysqld restartsUUID_SHORT()
的次数少于1600万次
The UUID_SHORT()
return value is constructed this way:UUID_SHORT()
返回值的构造方式如下:
(server_id & 255) << 56 + (server_startup_time_in_seconds << 24) + incremented_variable++;
mysql> SELECT UUID_SHORT();
-> 92395783831158784
UUID_SHORT()
does not work with statement-based replication.UUID_SHORT()
不适用于基于语句的复制。
UUID_TO_BIN(
, string_uuid
)UUID_TO_BIN(
string_uuid
, swap_flag
)
Converts a string UUID to a binary UUID and returns the result. 将字符串UUID转换为二进制UUID并返回结果。(The (IS_UUID()
function description lists the permitted string UUID formats.) IS_UUID()
函数说明列出了允许的字符串UUID格式。)The return binary UUID is a 返回的二进制UUID是VARBINARY(16)
value. VARBINARY(16)
值。If the UUID argument is 如果UUID参数为NULL
, the return value is NULL
. NULL
,则返回值为NULL
。If any argument is invalid, an error occurs.如果任何参数无效,则会发生错误。
UUID _TO UUID_TO_BIN()
takes one or two arguments:_BIN()
接受一个或两个参数:
The one-argument form takes a string UUID value. 单参数形式采用字符串UUID值。The binary result is in the same order as the string argument.二进制结果与字符串参数的顺序相同。
The two-argument form takes a string UUID value and a flag value:双参数形式采用字符串UUID值和标志值:
If 如果swap_flag
is 0, the two-argument form is equivalent to the one-argument form. The binary result is in the same order as the string argument.swap_flag
为0,则双参数形式等效于单参数形式。二进制结果与字符串参数的顺序相同。
If 如果swap_flag
is 1, the format of the return value differs: The time-low and time-high parts (the first and third groups of hexadecimal digits, respectively) are swapped. swap_flag
为1,则返回值的格式不同:时间低位和时间高位部分(分别是十六进制数字的第一组和第三组)被交换。This moves the more rapidly varying part to the right and can improve indexing efficiency if the result is stored in an indexed column.这会将变化较快的部分向右移动,如果结果存储在索引列中,则可以提高索引效率。
Time-part swapping assumes the use of UUID version 1 values, such as are generated by the 时间部分交换假定使用UUID version 1值,例如由UUID()
function. UUID()
函数生成的值。For UUID values produced by other means that do not follow version 1 format, time-part swapping provides no benefit. 对于由其他不遵循版本1格式的方法生成的UUID值,时间部分交换没有任何好处。For details about version 1 format, see the 有关版本1格式的详细信息,请参阅UUID()
function description.UUID()
函数说明。
Suppose that you have the following string UUID value:假设您具有以下字符串UUID值:
mysql> SET @uuid = '6ccd780c-baba-1026-9564-5b8c656024db';
To convert the string UUID to binary with or without time-part swapping, use 要将字符串UUID转换为二进制(带或不带时间部分交换),请使用UUID_TO_BIN()
:UUID_TO_BIN()
:
mysql>SELECT HEX(UUID_TO_BIN(@uuid));
+----------------------------------+ | HEX(UUID_TO_BIN(@uuid)) | +----------------------------------+ | 6CCD780CBABA102695645B8C656024DB | +----------------------------------+ mysql>SELECT HEX(UUID_TO_BIN(@uuid, 0));
+----------------------------------+ | HEX(UUID_TO_BIN(@uuid, 0)) | +----------------------------------+ | 6CCD780CBABA102695645B8C656024DB | +----------------------------------+ mysql>SELECT HEX(UUID_TO_BIN(@uuid, 1));
+----------------------------------+ | HEX(UUID_TO_BIN(@uuid, 1)) | +----------------------------------+ | 1026BABA6CCD780C95645B8C656024DB | +----------------------------------+
To convert a binary UUID returned by 要将UUID_TO_BIN()
to a string UUID, use BIN_TO_UUID()
. UUID_TO_BIN()
返回的二进制UUID转换为字符串UUID,请使用BIN_TO_UUID()
。If you produce a binary UUID by calling 如果通过使用第二个参数1调用UUID_TO_BIN()
with a second argument of 1 to swap time parts, you should also pass a second argument of 1 to BIN_TO_UUID()
to unswap the time parts when converting the binary UUID back to a string UUID:UUID_TO_BIN()
以交换时间部分来生成二进制UUID,则在将二进制UUID转换回字符串UUID时,还应将第二个参数1传递给BIN_TO_UUID()
,以取消时间部分的交换:
mysql>SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid));
+--------------------------------------+ | BIN_TO_UUID(UUID_TO_BIN(@uuid)) | +--------------------------------------+ | 6ccd780c-baba-1026-9564-5b8c656024db | +--------------------------------------+ mysql>SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid,0),0);
+--------------------------------------+ | BIN_TO_UUID(UUID_TO_BIN(@uuid,0),0) | +--------------------------------------+ | 6ccd780c-baba-1026-9564-5b8c656024db | +--------------------------------------+ mysql>SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid,1),1);
+--------------------------------------+ | BIN_TO_UUID(UUID_TO_BIN(@uuid,1),1) | +--------------------------------------+ | 6ccd780c-baba-1026-9564-5b8c656024db | +--------------------------------------+
If the use of time-part swapping is not the same for the conversion in both directions, the original UUID is not recovered properly:如果两个方向的转换使用的时间部分交换不同,则原始UUID无法正确恢复:
mysql>SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid,0),1);
+--------------------------------------+ | BIN_TO_UUID(UUID_TO_BIN(@uuid,0),1) | +--------------------------------------+ | baba1026-780c-6ccd-9564-5b8c656024db | +--------------------------------------+ mysql>SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid,1),0);
+--------------------------------------+ | BIN_TO_UUID(UUID_TO_BIN(@uuid,1),0) | +--------------------------------------+ | 1026baba-6ccd-780c-9564-5b8c656024db | +--------------------------------------+
If 如果从mysql客户机中调用UUID_TO_BIN()
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex
. UUID_TO_BIN()
,则二进制字符串将使用十六进制表示法显示,具体取决于--binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql-mysql命令行客户端”。
In an 在INSERT ... ON DUPLICATE KEY UPDATE
statement, you can use the VALUES(
function in the col_name
)UPDATE
clause to refer to column values from the INSERT
portion of the statement. INSERT ... ON DUPLICATE KEY UPDATE
语句中,可以使用UPDATE
子句中的VALUES(
函数引用语句col_name
)INSERT
部分的列值。In other words, 换句话说,VALUES(
in the col_name
)UPDATE
clause refers to the value of col_name
that would be inserted, had no duplicate-key conflict occurred. UPDATE
子句中的VALUES(
指的是在没有出现重复键冲突的情况下将插入的col_name
)col_name
的值。This function is especially useful in multiple-row inserts. 此函数在多行插入中特别有用。The VALUES()
function is meaningful only in the ON DUPLICATE KEY UPDATE
clause of INSERT
statements and returns NULL
otherwise. VALUES()
函数仅在INSERT
语句的ON DUPLICATE KEY UPDATE
子句中有意义,否则返回NULL
。See Section 13.2.6.2, “INSERT ... ON DUPLICATE KEY UPDATE Statement”.见第13.2.6.2节,“INSERT ... ON DUPLICATE KEY UPDATE语句”。
mysql>INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
->ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
This usage is deprecated in MySQL 8.0.20, and is subject to removal in a future release of MySQL. 这种用法在MySQL 8.0.20中已被弃用,在MySQL的未来版本中可能会被删除。Use a row alias, or row and column aliases, instead. 请改用行别名或行和列别名。See Section 13.2.6.2, “INSERT ... ON DUPLICATE KEY UPDATE Statement”, for more information and examples.有关详细信息和示例请参阅第13.2.6.2节,“INSERT ... ON DUPLICATE KEY UPDATE语句”。