参考:
https://dev.mysql.com/doc/refman/8.0/en/type-conversion.html 隐式转换
https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html mysql 类型转换函数 cast、convert、binary
隐式转换,就是指把不同类型的字段放在一起做运算(数学运算、比较运算等),会发生类型转换以使操作数兼容。

  1. CREATE TABLE `test_tempral` (
  2. `t_time` time DEFAULT NULL COMMENT 'time 时间',
  3. `t_date` date DEFAULT NULL COMMENT 'date 日期',
  4. `t_datetime` datetime DEFAULT NULL COMMENT 'datetime 日期时间',
  5. `t_year` year DEFAULT NULL COMMENT 'year 年',
  6. `t_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp 时间戳',
  7. `id` int NOT NULL AUTO_INCREMENT,
  8. `e` enum('a','b','c') CHARACTER SET binary COLLATE binary DEFAULT 'a',
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

1 字符串与数字

1.1 测试 sql (逻辑符号 =、 >、 <、 >=、 <=)

  1. select 1 = '1', '1a' = '1', 'a' = 1, '1a' = 1, 'a1' =1, 1= '1a';
  2. -- 结果
  3. +---------+------------+---------+----------+---------+---------+
  4. | 1 = '1' | '1a' = '1' | 'a' = 1 | '1a' = 1 | 'a1' =1 | 1= '1a' |
  5. +---------+------------+---------+----------+---------+---------+
  6. | 1 | 0 | 0 | 1 | 0 | 1 |
  7. +---------+------------+---------+----------+---------+---------+
  8. 1 row in set
  9. mysql> select version();
  10. +-----------+
  11. | version() |
  12. +-----------+
  13. | 5.7.29 |
  14. +-----------+
  15. 1 row in set
  16. mysql> select 1 > '1', '1a' > '1', 'a' > 1, '1a' > 1, 'a1' > 1, 1 > '1a';
  17. +---------+------------+---------+----------+----------+----------+
  18. | 1 > '1' | '1a' > '1' | 'a' > 1 | '1a' > 1 | 'a1' > 1 | 1 > '1a' |
  19. +---------+------------+---------+----------+----------+----------+
  20. | 0 | 1 | 0 | 0 | 0 | 0 |
  21. +---------+------------+---------+----------+----------+----------+
  22. 1 row in set
  23. mysql> select 1 < '1', '1a' < '1', 'a' < 1, '1a' < 1, 'a1' < 1, 1 < '1a';
  24. +---------+------------+---------+----------+----------+----------+
  25. | 1 < '1' | '1a' < '1' | 'a' < 1 | '1a' < 1 | 'a1' < 1 | 1 < '1a' |
  26. +---------+------------+---------+----------+----------+----------+
  27. | 0 | 0 | 1 | 0 | 1 | 0 |
  28. +---------+------------+---------+----------+----------+----------+
  29. 1 row in set
  30. mysql> select 1 <= '1', '1a' <= '1', 'a' <= 1, '1a' <= 1, 'a1' <= 1, 1 <= '1a';
  31. +----------+-------------+----------+-----------+-----------+-----------+
  32. | 1 <= '1' | '1a' <= '1' | 'a' <= 1 | '1a' <= 1 | 'a1' <= 1 | 1 <= '1a' |
  33. +----------+-------------+----------+-----------+-----------+-----------+
  34. | 1 | 0 | 1 | 1 | 1 | 1 |
  35. +----------+-------------+----------+-----------+-----------+-----------+
  36. 1 row in set
  37. mysql> select 1 >= '1', '1a' >= '1', 'a' >= 1, '1a' >= 1, 'a1' >= 1, 1 >= '1a';
  38. +----------+-------------+----------+-----------+-----------+-----------+
  39. | 1 >= '1' | '1a' >= '1' | 'a' >= 1 | '1a' >= 1 | 'a1' >= 1 | 1 >= '1a' |
  40. +----------+-------------+----------+-----------+-----------+-----------+
  41. | 1 | 1 | 0 | 1 | 0 | 1 |
  42. +----------+-------------+----------+-----------+-----------+-----------+
  43. 1 row in set
  44. mysql>

1.2 测试 SQL (+ 加号)

  1. mysql> SELECT
  2. concat(1024, 'abcd'),
  3. CONCAT('abcd', 1024),
  4. 1024 + 'abcd',
  5. 'abcd' + 1024,
  6. 1024 + '1abcd',
  7. '1abcd' + 1024,
  8. 1024 + 'a1bcd',
  9. 'a1bcd' + 1024,
  10. 1024 + 'abcd1',
  11. 'abcd1' + 1024,
  12. VERSION();
  13. +----------------------+----------------------+---------------+---------------+----------------+----------------+----------------+----------------+----------------+----------------+-----------+
  14. | concat(1024, 'abcd') | CONCAT('abcd', 1024) | 1024 + 'abcd' | 'abcd' + 1024 | 1024 + '1abcd' | '1abcd' + 1024 | 1024 + 'a1bcd' | 'a1bcd' + 1024 | 1024 + 'abcd1' | 'abcd1' + 1024 | VERSION() |
  15. +----------------------+----------------------+---------------+---------------+----------------+----------------+----------------+----------------+----------------+----------------+-----------+
  16. | 1024abcd | abcd1024 | 1024 | 1024 | 1025 | 1025 | 1024 | 1024 | 1024 | 1024 | 5.7.21 |
  17. +----------------------+----------------------+---------------+---------------+----------------+----------------+----------------+----------------+----------------+----------------+-----------+
  18. 1 row in set
  19. mysql>
  20. mysql> SELECT
  21. concat(1024, 'abcd') AS a1,
  22. CONCAT('abcd', 1024) AS a2,
  23. 1024 + 'abcd' AS a3,
  24. 'abcd' + 1024 AS a4,
  25. 1024 + '1abcd' AS a5,
  26. '1abcd' + 1024 AS a6,
  27. 1024 + 'a1bcd' AS a7,
  28. 'a1bcd' + 1024 AS a8,
  29. 1024 + 'abcd1' AS a9,
  30. 'abcd1' + 1024 AS a10,
  31. VERSION() AS v;
  32. +----------+----------+------+------+------+------+------+------+------+------+--------+
  33. | a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 | v |
  34. +----------+----------+------+------+------+------+------+------+------+------+--------+
  35. | 1024abcd | abcd1024 | 1024 | 1024 | 1025 | 1025 | 1024 | 1024 | 1024 | 1024 | 5.7.21 |
  36. +----------+----------+------+------+------+------+------+------+------+------+--------+
  37. 1 row in set
  38. mysql>

1.3 测试 SQL(- 减号)

  1. mysql> SELECT
  2. concat(1024, 'abcd') AS a1,
  3. CONCAT('abcd', 1024) AS a2,
  4. 1024 - 'abcd' AS a3,
  5. 'abcd' - 1024 AS a4,
  6. 1024 - '1abcd' AS a5,
  7. '1abcd' - 1024 AS a6,
  8. 1024 - 'a1bcd' AS a7,
  9. 'a1bcd' - 1024 AS a8,
  10. 1024 - 'abcd1' AS a9,
  11. 'abcd1' - 1024 AS a10,
  12. VERSION() AS v;
  13. +----------+----------+------+-------+------+-------+------+-------+------+-------+--------+
  14. | a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 | v |
  15. +----------+----------+------+-------+------+-------+------+-------+------+-------+--------+
  16. | 1024abcd | abcd1024 | 1024 | -1024 | 1023 | -1023 | 1024 | -1024 | 1024 | -1024 | 5.7.21 |
  17. +----------+----------+------+-------+------+-------+------+-------+------+-------+--------+
  18. 1 row in set
  19. mysql>

1.4 测试 SQL (* 乘号)

  1. mysql> SELECT
  2. concat(1024, 'abcd') AS a1,
  3. CONCAT('abcd', 1024) AS a2,
  4. 1024 * 'abcd' AS a3,
  5. 'abcd' * 1024 AS a4,
  6. 1024 * '1abcd' AS a5,
  7. '1abcd' * 1024 AS a6,
  8. 1024 * 'a1bcd' AS a7,
  9. 'a1bcd' * 1024 AS a8,
  10. 1024 * 'abcd1' AS a9,
  11. 'abcd1' * 1024 AS a10,
  12. VERSION() AS v;
  13. +----------+----------+----+----+------+------+----+----+----+-----+--------+
  14. | a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 | v |
  15. +----------+----------+----+----+------+------+----+----+----+-----+--------+
  16. | 1024abcd | abcd1024 | 0 | 0 | 1024 | 1024 | 0 | 0 | 0 | 0 | 5.7.21 |
  17. +----------+----------+----+----+------+------+----+----+----+-----+--------+
  18. 1 row in set
  19. mysql>

1.5 测试 SQL (/ 除号)

  1. mysql> SELECT
  2. concat(1024, 'abcd') AS a1,
  3. CONCAT('abcd', 1024) AS a2,
  4. 1024 / 'abcd' AS a3,
  5. 'abcd' / 1024 AS a4,
  6. 1024 / '1abcd' AS a5,
  7. '1abcd' / 1024 AS a6,
  8. 1024 / 'a1bcd' AS a7,
  9. 'a1bcd' / 1024 AS a8,
  10. 1024 / 'abcd1' AS a9,
  11. 'abcd1' / 1024 AS a10,
  12. VERSION() AS v;
  13. +----------+----------+------+----+------+--------------+------+----+------+-----+--------+
  14. | a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 | a9 | a10 | v |
  15. +----------+----------+------+----+------+--------------+------+----+------+-----+--------+
  16. | 1024abcd | abcd1024 | NULL | 0 | 1024 | 0.0009765625 | NULL | 0 | NULL | 0 | 5.7.21 |
  17. +----------+----------+------+----+------+--------------+------+----+------+-----+--------+
  18. 1 row in set
  19. mysql>

1.6 其它()数学运算、like、字符串与小数等

  1. mysql> select POW(2,'3a'),POW(2,'a3'),COS('a1'),cos('-90a'), 1024 like '102%', 1 + 2.345, '1a' + 2.345, 'a1a' + 2.345;
  2. +-------------+-------------+-----------+---------------------+------------------+-----------+--------------+---------------+
  3. | POW(2,'3a') | POW(2,'a3') | COS('a1') | cos('-90a') | 1024 like '102%' | 1 + 2.345 | '1a' + 2.345 | 'a1a' + 2.345 |
  4. +-------------+-------------+-----------+---------------------+------------------+-----------+--------------+---------------+
  5. | 8 | 1 | 1 | -0.4480736161291701 | 1 | 3.345 | 3.345 | 2.345 |
  6. +-------------+-------------+-----------+---------------------+------------------+-----------+--------------+---------------+
  7. 1 row in set
  8. mysql>

1.7 小结

字符与数字比较,mysql 会先尝试将字符串转为数字比较,如果能够转为数字,则按数字比较,不能转为数字,则转换为数字 0 。故而,都会转为数字。

2 与null的相关测试

  1. mysql> select 1 = null, '1' = null, null = null, ISNULL(null), POW(null,2), POW(2,null), version();
  2. +----------+------------+-------------+--------------+-------------+-------------+-----------+
  3. | 1 = null | '1' = null | null = null | ISNULL(null) | POW(null,2) | POW(2,null) | version() |
  4. +----------+------------+-------------+--------------+-------------+-------------+-----------+
  5. | NULL | NULL | NULL | 1 | NULL | NULL | 5.7.21 |
  6. +----------+------------+-------------+--------------+-------------+-------------+-----------+
  7. 1 row in set
  8. mysql>
  9. mysql> select null like '123%', isnull(null like '123%'), pow(2, null), ISNULL(pow(2, null)), version();
  10. select 123 like 123, '123' like 123, '123a' like 123, 123 like '12%', isnull(null like '123%'), version();
  11. +------------------+--------------------------+--------------+----------------------+-----------+
  12. | null like '123%' | isnull(null like '123%') | pow(2, null) | ISNULL(pow(2, null)) | version() |
  13. +------------------+--------------------------+--------------+----------------------+-----------+
  14. | NULL | 1 | NULL | 1 | 5.7.21 |
  15. +------------------+--------------------------+--------------+----------------------+-----------+
  16. 1 row in set
  17. +--------------+----------------+-----------------+----------------+--------------------------+-----------+
  18. | 123 like 123 | '123' like 123 | '123a' like 123 | 123 like '12%' | isnull(null like '123%') | version() |
  19. +--------------+----------------+-----------------+----------------+--------------------------+-----------+
  20. | 1 | 1 | 0 | 1 | 1 | 5.7.21 |
  21. +--------------+----------------+-----------------+----------------+--------------------------+-----------+
  22. 1 row in set
  23. mysql>

在5.7 中,所有的与 null 操作都是 null,比较 null 需要使用 isnull() 函数。

3 小结数字

数字与字符串相关操作,会发生隐式转换,至于是数字转字符串,还是字符串转数字,根据不同的场景来看。数学运算操作(=、>、<、+、-、*、/、pow、cos、sin 等),会将字符串转数字;like、concat 等会将数字转字符串。记不住也没关系,随便来个例子就得到结果:

  1. mysql> select concat(2.345,'aa'),concat(2.345 + '1a',2.345 + 'a1','aa'),concat(2.345 , 2.1);
  2. +--------------------+----------------------------------------+---------------------+
  3. | concat(2.345,'aa') | concat(2.345 + '1a',2.345 + 'a1','aa') | concat(2.345 , 2.1) |
  4. +--------------------+----------------------------------------+---------------------+
  5. | 2.345aa | 3.3452.345aa | 2.3452.1 |
  6. +--------------------+----------------------------------------+---------------------+
  7. 1 row in set
  8. mysql>

数字与数字之间也存在,相关操作会转为高精度的类型(未得官网证实,猜的)。

4. 字符串与日期时间

If one of the arguments is a TIMESTAMP or DATETIME column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed. This is done to be more ODBC-friendly. This is not done for the arguments to IN(). To be safe, always use complete datetime, date, or time strings when doing comparisons. For example, to achieve best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.

  1. mysql> select cast('01:00:00' as DATETIME), cast('2021-09-01' as DATETIME), cast('20210901' as DATETIME),VERSION();
  2. select cast('01:00:00' as DATE), cast('2021-09-01' as DATE), cast('20210901' as DATE),VERSION();
  3. select cast('01:00:00' as TIME), cast('2021-09-01' as TIME), cast('20210901' as TIME),VERSION();
  4. +------------------------------+--------------------------------+------------------------------+------------+
  5. | cast('01:00:00' as DATETIME) | cast('2021-09-01' as DATETIME) | cast('20210901' as DATETIME) | VERSION() |
  6. +------------------------------+--------------------------------+------------------------------+------------+
  7. | 2001-00-00 00:00:00 | 2021-09-01 00:00:00 | 2021-09-01 00:00:00 | 5.7.29-log |
  8. +------------------------------+--------------------------------+------------------------------+------------+
  9. 1 row in set (0.02 sec)
  10. +--------------------------+----------------------------+--------------------------+------------+
  11. | cast('01:00:00' as DATE) | cast('2021-09-01' as DATE) | cast('20210901' as DATE) | VERSION() |
  12. +--------------------------+----------------------------+--------------------------+------------+
  13. | 2001-00-00 | 2021-09-01 | 2021-09-01 | 5.7.29-log |
  14. +--------------------------+----------------------------+--------------------------+------------+
  15. 1 row in set (0.01 sec)
  16. +--------------------------+----------------------------+--------------------------+------------+
  17. | cast('01:00:00' as TIME) | cast('2021-09-01' as TIME) | cast('20210901' as TIME) | VERSION() |
  18. +--------------------------+----------------------------+--------------------------+------------+
  19. | 01:00:00 | 00:20:21 | 838:59:59 | 5.7.29-log |
  20. +--------------------------+----------------------------+--------------------------+------------+
  21. 1 row in set (0.02 sec)
  22. mysql>

字符串与时间的测试

  1. mysql> select now(), now() between '2021-01-01 12:00:00' and '2021-01-01 16:00:00';
  2. select now(), now() between '2021-01-01 12:00:00' and '2021-01-01 16:00:00';
  3. select CURTIME(), CURTIME() between '2021-01-01 12:00:00' and '2021-01-01 16:00:00';
  4. select CURRENT_TIME(), CURRENT_TIME() between '2021-01-01 12:00:00' and '2021-01-01 16:00:00';
  5. select CURRENT_TIME(), CURRENT_TIME() between '0000-00-00 12:00:00' and '2021-01-01 16:00:00';
  6. select CURRENT_TIME(), CURRENT_TIME() between '0000-00-00 12:00:00' and '1997-00-00 16:00:00';
  7. select CURRENT_DATE(), CURRENT_DATE() between '2021-09-08 12:00:00' and '2021-09-08 11:00:00';
  8. select CURRENT_DATE(), CURRENT_DATE() between '2021-09-08 00:00:00' and '2021-09-08 11:00:00';
  9. select CURRENT_DATE(), CURRENT_DATE() between '2021-09-08 00:00:00' and '2021-09-08 00:00:00';
  10. select CURDATE(),CURDATE() between '2021-09-08 00:00:00' and '2021-09-08 11:00:00';
  11. select CURDATE(),CURDATE() between '2021-09-08 00:00:00' and '2021-09-08 00:00:00';
  12. select CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP() between '2021-09-08 00:00:00' and '2021-09-11 11:00:00';
  13. select CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP() between '2021-09-08 29:90:70' and '2021-09-09';
  14. +---------------------+---------------------------------------------------------------+
  15. | now() | now() between '2021-01-01 12:00:00' and '2021-01-01 16:00:00' |
  16. +---------------------+---------------------------------------------------------------+
  17. | 2021-09-08 16:17:03 | 0 |
  18. +---------------------+---------------------------------------------------------------+
  19. 1 row in set (0.02 sec)
  20. +---------------------+---------------------------------------------------------------+
  21. | now() | now() between '2021-01-01 12:00:00' and '2021-01-01 16:00:00' |
  22. +---------------------+---------------------------------------------------------------+
  23. | 2021-09-08 16:17:03 | 0 |
  24. +---------------------+---------------------------------------------------------------+
  25. 1 row in set (0.02 sec)
  26. +-----------+-------------------------------------------------------------------+
  27. | CURTIME() | CURTIME() between '2021-01-01 12:00:00' and '2021-01-01 16:00:00' |
  28. +-----------+-------------------------------------------------------------------+
  29. | 16:17:03 | 0 |
  30. +-----------+-------------------------------------------------------------------+
  31. 1 row in set (0.02 sec)
  32. +----------------+------------------------------------------------------------------------+
  33. | CURRENT_TIME() | CURRENT_TIME() between '2021-01-01 12:00:00' and '2021-01-01 16:00:00' |
  34. +----------------+------------------------------------------------------------------------+
  35. | 16:17:03 | 0 |
  36. +----------------+------------------------------------------------------------------------+
  37. 1 row in set (0.02 sec)
  38. +----------------+------------------------------------------------------------------------+
  39. | CURRENT_TIME() | CURRENT_TIME() between '0000-00-00 12:00:00' and '2021-01-01 16:00:00' |
  40. +----------------+------------------------------------------------------------------------+
  41. | 16:17:03 | 1 |
  42. +----------------+------------------------------------------------------------------------+
  43. 1 row in set (0.02 sec)
  44. +----------------+------------------------------------------------------------------------+
  45. | CURRENT_TIME() | CURRENT_TIME() between '0000-00-00 12:00:00' and '1997-00-00 16:00:00' |
  46. +----------------+------------------------------------------------------------------------+
  47. | 16:17:03 | 1 |
  48. +----------------+------------------------------------------------------------------------+
  49. 1 row in set (0.02 sec)
  50. +----------------+------------------------------------------------------------------------+
  51. | CURRENT_DATE() | CURRENT_DATE() between '2021-09-08 12:00:00' and '2021-09-08 11:00:00' |
  52. +----------------+------------------------------------------------------------------------+
  53. | 2021-09-08 | 0 |
  54. +----------------+------------------------------------------------------------------------+
  55. 1 row in set (0.04 sec)
  56. +----------------+------------------------------------------------------------------------+
  57. | CURRENT_DATE() | CURRENT_DATE() between '2021-09-08 00:00:00' and '2021-09-08 11:00:00' |
  58. +----------------+------------------------------------------------------------------------+
  59. | 2021-09-08 | 1 |
  60. +----------------+------------------------------------------------------------------------+
  61. 1 row in set (0.04 sec)
  62. +----------------+------------------------------------------------------------------------+
  63. | CURRENT_DATE() | CURRENT_DATE() between '2021-09-08 00:00:00' and '2021-09-08 00:00:00' |
  64. +----------------+------------------------------------------------------------------------+
  65. | 2021-09-08 | 1 |
  66. +----------------+------------------------------------------------------------------------+
  67. 1 row in set (0.05 sec)
  68. +------------+-------------------------------------------------------------------+
  69. | CURDATE() | CURDATE() between '2021-09-08 00:00:00' and '2021-09-08 11:00:00' |
  70. +------------+-------------------------------------------------------------------+
  71. | 2021-09-08 | 1 |
  72. +------------+-------------------------------------------------------------------+
  73. 1 row in set (0.04 sec)
  74. +------------+-------------------------------------------------------------------+
  75. | CURDATE() | CURDATE() between '2021-09-08 00:00:00' and '2021-09-08 00:00:00' |
  76. +------------+-------------------------------------------------------------------+
  77. | 2021-09-08 | 1 |
  78. +------------+-------------------------------------------------------------------+
  79. 1 row in set (0.04 sec)
  80. +---------------------+-----------------------------------------------------------------------------+
  81. | CURRENT_TIMESTAMP() | CURRENT_TIMESTAMP() between '2021-09-08 00:00:00' and '2021-09-11 11:00:00' |
  82. +---------------------+-----------------------------------------------------------------------------+
  83. | 2021-09-08 16:17:03 | 1 |
  84. +---------------------+-----------------------------------------------------------------------------+
  85. 1 row in set (0.04 sec)
  86. +---------------------+--------------------------------------------------------------------+
  87. | CURRENT_TIMESTAMP() | CURRENT_TIMESTAMP() between '2021-09-08 29:90:70' and '2021-09-09' |
  88. +---------------------+--------------------------------------------------------------------+
  89. | 2021-09-08 16:17:04 | 1 |
  90. +---------------------+--------------------------------------------------------------------+
  91. 1 row in set (0.05 sec)
  92. mysql>
  93. mysql> select CURRENT_TIME(), CURRENT_TIMESTAMP() > '2021';
  94. 1525 - Incorrect DATETIME value: '2021'
  95. mysql> select CURRENT_TIME(), CURRENT_TIMESTAMP() > '2021-09-01';
  96. +----------------+------------------------------------+
  97. | CURRENT_TIME() | CURRENT_TIMESTAMP() > '2021-09-01' |
  98. +----------------+------------------------------------+
  99. | 16:21:38 | 1 |
  100. +----------------+------------------------------------+
  101. 1 row in set (0.05 sec)
  102. mysql> select CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP() > '10:20:59';
  103. 1525 - Incorrect DATETIME value: '10:20:59'
  104. mysql> select CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP() > '2021-09-09 10:20:59';
  105. +---------------------+---------------------------------------------+
  106. | CURRENT_TIMESTAMP() | CURRENT_TIMESTAMP() > '2021-09-09 10:20:59' |
  107. +---------------------+---------------------------------------------+
  108. | 2021-09-08 16:22:36 | 0 |
  109. +---------------------+---------------------------------------------+
  110. 1 row in set (0.05 sec)
  111. mysql> select sql_mode;
  112. 1054 - Unknown column 'sql_mode' in 'field list'
  113. mysql> select @@session.sql_mode, @@global.sql_mode;
  114. +-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
  115. | @@session.sql_mode | @@global.sql_mode |
  116. +-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
  117. | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
  118. +-----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
  119. 1 row in set (0.05 sec)
  120. mysql>
  121. mysql> select now(),now() > 0, now()>22222222222222222222222, TIMESTAMP(now()), GET_FORMAT(TIME, 'ISO'), hour(now()), minute(now()), SECOND(now()),DAYOFYEAR(now()),
  122. dayofweek(now()),DAYOFMONTH(now()),dayname(now()), day(now()),FROM_DAYS(367),QUARTER(LOCALTIME),EXTRACT(MICROSECOND from now(4));
  123. +---------------------+-----------+-------------------------------+---------------------+-------------------------+-------------+---------------+---------------+------------------+------------------+-------------------+----------------+------------+----------------+--------------------+----------------------------------+
  124. | now() | now() > 0 | now()>22222222222222222222222 | TIMESTAMP(now()) | GET_FORMAT(TIME, 'ISO') | hour(now()) | minute(now()) | SECOND(now()) | DAYOFYEAR(now()) | dayofweek(now()) | DAYOFMONTH(now()) | dayname(now()) | day(now()) | FROM_DAYS(367) | QUARTER(LOCALTIME) | EXTRACT(MICROSECOND from now(4)) |
  125. +---------------------+-----------+-------------------------------+---------------------+-------------------------+-------------+---------------+---------------+------------------+------------------+-------------------+----------------+------------+----------------+--------------------+----------------------------------+
  126. | 2021-09-08 17:35:17 | 1 | 0 | 2021-09-08 17:35:17 | %H:%i:%s | 17 | 35 | 17 | 251 | 4 | 8 | Wednesday | 8 | 0001-01-02 | 3 | 310700 |
  127. +---------------------+-----------+-------------------------------+---------------------+-------------------------+-------------+---------------+---------------+------------------+------------------+-------------------+----------------+------------+----------------+--------------------+----------------------------------+
  128. 1 row in set (0.07 sec)
  129. mysql>

字符串与时间比较,将会将字符串转为对应的时间格式(转换不成功,有的场景要报错)。

5 时间与数字

  1. mysql> select LOCALTIME > 1, LOCALTIME() > 'a1';
  2. 1525 - Incorrect DATETIME value: 'a1'
  3. mysql>

6. 类型转换函数 cast

参考: https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_cast

6.1 语法

CAST(exprAStype[ARRAY])
CAST(timestamp_valueAT TIME ZONEtimezone_specifierAS DATETIME[(precision)])
timezone_specifier: [INTERVAL] ‘+00:00’ | ‘UTC’
With CAST(exprAStype syntax, the CAST() function takes an expression of any type and produces a result value of the specified type. This operation may also be expressed as CONVERT(expr,type), which is equivalent.

7. 类型转换函数 convert

参考: https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_convert

7.1 语法

CONVERT(exprUSINGtranscoding_name)
CONVERT(expr,type)
CONVERT(exprUSINGtranscoding_name) is standard SQL syntax. The non-USING form of CONVERT() is ODBC syntax.
CONVERT(exprUSINGtranscoding_name) converts data between different character sets. In MySQL, transcoding names are the same as the corresponding character set names. For example, this statement converts the string ‘abc’ in the default character set to the corresponding string in the utf8mb4 character set:
SELECT CONVERT(‘abc’USING utf8mb4);
CONVERT(expr,type) syntax (without USING) takes an expression and a type value specifying a result type, and produces a result value of the specified type. This operation may also be expressed as CAST(exprAStype), which is equivalent. For more information, see the description of CAST().

总结

转换规则多,尽量不要去使用隐式转换,明明白白地指定格式。