如何在 MySQL 中反转 EXTRACT() 的输出
How to reverse the output of EXTRACT() in MySQL
我有以下查询 -
SELECT NOW(6) AS output1, CONCAT(EXTRACT(YEAR_MONTH FROM NOW()), EXTRACT(DAY FROM NOW()), EXTRACT(HOUR_MICROSECOND FROM NOW(6))) AS output2;
+----------------------------+--------------------+
| output1 | output2 |
+----------------------------+--------------------+
| 2021-12-07 02:06:28.783259 | 202112720628783259 |
+----------------------------+--------------------+
现在我想将结果 - 202112720628783259
反转为 2021-12-07 02:06:28.783259
。但是我想不出办法。
在output2
中,日、时、分都是一位数。所以你不能可靠地将它解析回 YYYY-MM-DD HH:MM:SS.
我建议您改为使用 DATE_FORMAT() and its reverse, STR_TO_DATE()。
SELECT NOW(6) AS output1, DATE_FORMAT(NOW(6), '%Y%m%d%H%i%s%f') as output2
+----------------------------+----------------------+
| output1 | output2 |
+----------------------------+----------------------+
| 2021-12-06 20:44:31.927978 | 20211206204431927978 |
+----------------------------+----------------------+
我有以下查询 -
SELECT NOW(6) AS output1, CONCAT(EXTRACT(YEAR_MONTH FROM NOW()), EXTRACT(DAY FROM NOW()), EXTRACT(HOUR_MICROSECOND FROM NOW(6))) AS output2;
+----------------------------+--------------------+
| output1 | output2 |
+----------------------------+--------------------+
| 2021-12-07 02:06:28.783259 | 202112720628783259 |
+----------------------------+--------------------+
现在我想将结果 - 202112720628783259
反转为 2021-12-07 02:06:28.783259
。但是我想不出办法。
在output2
中,日、时、分都是一位数。所以你不能可靠地将它解析回 YYYY-MM-DD HH:MM:SS.
我建议您改为使用 DATE_FORMAT() and its reverse, STR_TO_DATE()。
SELECT NOW(6) AS output1, DATE_FORMAT(NOW(6), '%Y%m%d%H%i%s%f') as output2
+----------------------------+----------------------+
| output1 | output2 |
+----------------------------+----------------------+
| 2021-12-06 20:44:31.927978 | 20211206204431927978 |
+----------------------------+----------------------+