Miscellaneous Functions

TiDB supports most of the miscellaneous functions available in MySQL 8.0.

Supported functions

NameDescription
ANY_VALUE()Suppress ONLY_FULL_GROUP_BY value rejection
BIN_TO_UUID()Convert UUID from binary format to text format
DEFAULT()Returns the default value for a table column
GROUPING()Modifier for GROUP BY operations
INET_ATON()Return the numeric value of an IP address
INET_NTOA()Return the IP address from a numeric value
INET6_ATON()Return the numeric value of an IPv6 address
INET6_NTOA()Return the IPv6 address from a numeric value
IS_IPV4()Whether argument is an IPv4 address
IS_IPV4_COMPAT()Whether argument is an IPv4-compatible address
IS_IPV4_MAPPED()Whether argument is an IPv4-mapped address
IS_IPV6()Whether argument is an IPv6 address
IS_UUID()Whether argument is an UUID
NAME_CONST()Can be used to rename a column name
SLEEP()Sleep for a number of seconds. Note that for TiDB Serverless clusters, the SLEEP() function has a limitation wherein it can only support a maximum sleep time of 300 seconds.
UUID()Return a Universal Unique Identifier (UUID)
UUID_TO_BIN()Convert UUID from text format to binary format
VALUES()Defines the values to be used during an INSERT

ANY_VALUE()

The ANY_VALUE() function returns any value from a group of values. Typically, it is used in scenarios where you need to include non-aggregated columns in your SELECT statement along with a GROUP BY clause.

  1. CREATE TABLE fruits (id INT PRIMARY KEY, name VARCHAR(255));
  2. Query OK, 0 rows affected (0.14 sec)
  3. INSERT INTO fruits VALUES (1,'apple'),(2,'apple'),(3,'pear'),(4,'banana'),(5, 'pineapple');
  4. Query OK, 5 rows affected (0.01 sec)
  5. Records: 5 Duplicates: 0 Warnings: 0
  6. SELECT id,name FROM fruits GROUP BY name;
  7. ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.fruits.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
  8. SELECT ANY_VALUE(id),GROUP_CONCAT(id),name FROM fruits GROUP BY name;
  9. +---------------+------------------+-----------+
  10. | ANY_VALUE(id) | GROUP_CONCAT(id) | name |
  11. +---------------+------------------+-----------+
  12. | 1 | 1,2 | apple |
  13. | 3 | 3 | pear |
  14. | 4 | 4 | banana |
  15. | 5 | 5 | pineapple |
  16. +---------------+------------------+-----------+
  17. 4 rows in set (0.00 sec)

In the preceding example, TiDB returns an error for the first SELECT statement because the id column is non-aggregated and not included in the GROUP BY clause. To address the issue, the second SELECT query uses ANY_VALUE() to get any value from each group and uses GROUP_CONCAT() to concatenate all values of the id column within each group into a single string. This approach enables you to get one value from each group and all values of the group without changing the SQL mode for non-aggregated columns.

BIN_TO_UUID()

BIN_TO_UUID() and UUID_TO_BIN() can be used to convert between a textual format UUID and a binary format. Both functions accept two arguments.

  • The first argument specifies the value to be converted.
  • The second argument (optional) controls the ordering of the fields in the binary format.
  1. SET @a := UUID();
  2. Query OK, 0 rows affected (0.00 sec)
  3. SELECT @a;
  4. +--------------------------------------+
  5. | @a |
  6. +--------------------------------------+
  7. | 9a17b457-eb6d-11ee-bacf-5405db7aad56 |
  8. +--------------------------------------+
  9. 1 row in set (0.00 sec)
  10. SELECT UUID_TO_BIN(@a);
  11. +------------------------------------+
  12. | UUID_TO_BIN(@a) |
  13. +------------------------------------+
  14. | 0x9A17B457EB6D11EEBACF5405DB7AAD56 |
  15. +------------------------------------+
  16. 1 row in set (0.00 sec)
  17. SELECT BIN_TO_UUID(0x9A17B457EB6D11EEBACF5405DB7AAD56);
  18. +-------------------------------------------------+
  19. | BIN_TO_UUID(0x9A17B457EB6D11EEBACF5405DB7AAD56) |
  20. +-------------------------------------------------+
  21. | 9a17b457-eb6d-11ee-bacf-5405db7aad56 |
  22. +-------------------------------------------------+
  23. 1 row in set (0.00 sec)
  24. SELECT UUID_TO_BIN(@a, 1);
  25. +----------------------------------------+
  26. | UUID_TO_BIN(@a, 1) |
  27. +----------------------------------------+
  28. | 0x11EEEB6D9A17B457BACF5405DB7AAD56 |
  29. +----------------------------------------+
  30. 1 row in set (0.00 sec)
  31. SELECT BIN_TO_UUID(0x11EEEB6D9A17B457BACF5405DB7AAD56, 1);
  32. +----------------------------------------------------+
  33. | BIN_TO_UUID(0x11EEEB6D9A17B457BACF5405DB7AAD56, 1) |
  34. +----------------------------------------------------+
  35. | 9a17b457-eb6d-11ee-bacf-5405db7aad56 |
  36. +----------------------------------------------------+
  37. 1 row in set (0.00 sec)

See also UUID() and Best practices for UUID.

DEFAULT()

The DEFAULT() function is used to get the default value of a column.

  1. CREATE TABLE t1 (id INT PRIMARY KEY, c1 INT DEFAULT 5);
  2. Query OK, 0 rows affected (0.15 sec)
  3. INSERT INTO t1 VALUES (1, 1);
  4. Query OK, 1 row affected (0.01 sec)
  5. UPDATE t1 SET c1=DEFAULT(c1)+3;
  6. Query OK, 1 row affected (0.02 sec)
  7. Rows matched: 1 Changed: 1 Warnings: 0
  8. TABLE t1;
  9. +----+------+
  10. | id | c1 |
  11. +----+------+
  12. | 1 | 8 |
  13. +----+------+
  14. 1 row in set (0.00 sec)

In the preceding example, the UPDATE statement sets the value of the c1 column to the default value of the column (which is 5) plus 3, resulting in a new value of 8.

GROUPING()

See GROUP BY modifiers.

INET_ATON()

The INET_ATON() function converts an IPv4 address in dotted-quad notation into a binary version that can be stored efficiently.

  1. SELECT INET_ATON('127.0.0.1');
  1. +------------------------+
  2. | INET_ATON('127.0.0.1') |
  3. +------------------------+
  4. | 2130706433 |
  5. +------------------------+
  6. 1 row in set (0.00 sec)

INET_NTOA()

The INET_NTOA() function converts a binary IPv4 address into a dotted-quad notation.

  1. SELECT INET_NTOA(2130706433);
  1. +-----------------------+
  2. | INET_NTOA(2130706433) |
  3. +-----------------------+
  4. | 127.0.0.1 |
  5. +-----------------------+
  6. 1 row in set (0.00 sec)

INET6_ATON()

The INET6_ATON() function is similar to INET_ATON(), but INET6_ATON() can also handle IPv6 addresses.

  1. SELECT INET6_ATON('::1');
  1. +--------------------------------------+
  2. | INET6_ATON('::1') |
  3. +--------------------------------------+
  4. | 0x00000000000000000000000000000001 |
  5. +--------------------------------------+
  6. 1 row in set (0.00 sec)

INET6_NTOA()

The INET6_NTOA() function is similar to INET_NTOA(), but INET6_NTOA() can also handle IPv6 addresses.

  1. SELECT INET6_NTOA(0x00000000000000000000000000000001);
  1. +------------------------------------------------+
  2. | INET6_NTOA(0x00000000000000000000000000000001) |
  3. +------------------------------------------------+
  4. | ::1 |
  5. +------------------------------------------------+
  6. 1 row in set (0.00 sec)

IS_IPV4()

The IS_IPV4() function tests whether the given argument is an IPv4 address or not.

  1. SELECT IS_IPV4('127.0.0.1');
  1. +----------------------+
  2. | IS_IPV4('127.0.0.1') |
  3. +----------------------+
  4. | 1 |
  5. +----------------------+
  6. 1 row in set (0.00 sec)
  1. SELECT IS_IPV4('300.0.0.1');
  1. +----------------------+
  2. | IS_IPV4('300.0.0.1') |
  3. +----------------------+
  4. | 0 |
  5. +----------------------+
  6. 1 row in set (0.00 sec)

IS_IPV4_COMPAT()

The IS_IPV4_COMPAT() function tests whether the given argument is an IPv4-compatible address.

  1. SELECT IS_IPV4_COMPAT(INET6_ATON('::127.0.0.1'));
  1. +-------------------------------------------+
  2. | IS_IPV4_COMPAT(INET6_ATON('::127.0.0.1')) |
  3. +-------------------------------------------+
  4. | 1 |
  5. +-------------------------------------------+
  6. 1 row in set (0.00 sec)

IS_IPV4_MAPPED()

The IS_IPV4_MAPPED() function tests whether the given argument is an IPv4-mapped address.

  1. SELECT IS_IPV4_MAPPED(INET6_ATON('::ffff:127.0.0.1'));
  1. +------------------------------------------------+
  2. | IS_IPV4_MAPPED(INET6_ATON('::ffff:127.0.0.1')) |
  3. +------------------------------------------------+
  4. | 1 |
  5. +------------------------------------------------+
  6. 1 row in set (0.00 sec)

IS_IPV6()

The IS_IPV6() function tests whether the given argument is an IPv6 address.

  1. SELECT IS_IPV6('::1');
  1. +----------------+
  2. | IS_IPV6('::1') |
  3. +----------------+
  4. | 1 |
  5. +----------------+
  6. 1 row in set (0.00 sec)

IS_UUID()

The IS_UUID() function tests whether the given argument is a UUID.

  1. SELECT IS_UUID('eb48c08c-eb71-11ee-bacf-5405db7aad56');
  1. +-------------------------------------------------+
  2. | IS_UUID('eb48c08c-eb71-11ee-bacf-5405db7aad56') |
  3. +-------------------------------------------------+
  4. | 1 |
  5. +-------------------------------------------------+
  6. 1 row in set (0.00 sec)

NAME_CONST()

The NAME_CONST() function is used to name columns. It is recommended to use column aliases instead.

  1. SELECT NAME_CONST('column name', 'value') UNION ALL SELECT 'another value';
  1. +---------------+
  2. | column name |
  3. +---------------+
  4. | another value |
  5. | value |
  6. +---------------+
  7. 2 rows in set (0.00 sec)

The preceding statement uses NAME_CONST() and the following statement uses the recommended way of aliasing columns.

  1. SELECT 'value' AS 'column name' UNION ALL SELECT 'another value';
  1. +---------------+
  2. | column name |
  3. +---------------+
  4. | value |
  5. | another value |
  6. +---------------+
  7. 2 rows in set (0.00 sec)

SLEEP()

The SLEEP() function is used to pause the execution of queries for a specified number of seconds.

  1. SELECT SLEEP(1.5);
  1. +------------+
  2. | SLEEP(1.5) |
  3. +------------+
  4. | 0 |
  5. +------------+
  6. 1 row in set (1.50 sec)

UUID()

The UUID() function returns a universally unique identifier (UUID) version 1 as defined in RFC 4122.

  1. SELECT UUID();
  1. +--------------------------------------+
  2. | UUID() |
  3. +--------------------------------------+
  4. | cb4d5ae6-eb6b-11ee-bacf-5405db7aad56 |
  5. +--------------------------------------+
  6. 1 row in set (0.00 sec)

See also Best practices for UUID.

UUID_TO_BIN

See BIN_TO_UUID().

VALUES()

The VALUES(col_name) function is used to reference the value of a specific column in the ON DUPLICATE KEY UPDATE clause of an INSERT statement.

  1. CREATE TABLE t1 (id INT PRIMARY KEY, c1 INT);
  2. Query OK, 0 rows affected (0.17 sec)
  3. INSERT INTO t1 VALUES (1,51),(2,52),(3,53),(4,54),(5,55);
  4. Query OK, 5 rows affected (0.01 sec)
  5. Records: 5 Duplicates: 0 Warnings: 0
  6. INSERT INTO t1 VALUES(2,22),(4,44) ON DUPLICATE KEY UPDATE c1=VALUES(id)+100;
  7. Query OK, 4 rows affected (0.01 sec)
  8. Records: 2 Duplicates: 2 Warnings: 0
  9. TABLE t1;
  10. +----+------+
  11. | id | c1 |
  12. +----+------+
  13. | 1 | 51 |
  14. | 2 | 102 |
  15. | 3 | 53 |
  16. | 4 | 104 |
  17. | 5 | 55 |
  18. +----+------+
  19. 5 rows in set (0.00 sec)

Unsupported functions

NameDescription
UUID_SHORT()Provides a UUID that is unique given certain assumptions not present in TiDB TiDB #4620