我正在尝试从 MySQL 中的 JSONFIELD“数据”中提取日期时间。
但是,如果我执行简单的 JSON_EXTRACT,则返回字段类型是 JSON。
mysql> select JSON_EXTRACT(data, "$.new_time") from analytics limit 10;
+----------------------------------+
| JSON_EXTRACT(data, "$.new_time") |
+----------------------------------+
| NULL |
| "2016-09-30T04:00:00+00:00" |
| "2016-09-29T05:30:00+00:00" |
| NULL |
| "2016-10-01T05:30:00+00:00" |
| "2016-09-27T23:00:00+00:00" |
| NULL |
| "2016-09-23T01:30:00+00:00" |
| "2016-09-23T04:00:00+00:00" |
| "2016-09-27T01:30:00+00:00" |
+----------------------------------+
我想将其转换为 MySQL DATETIME。但是如果我链接 JSON_EXTRACT 和 STR_TO_DATETIME,我会得到所有 NULL 值:
mysql> select STR_TO_DATE(JSON_EXTRACT(data, "$.new_time") ,"%Y-%m-%d") from analytics_calendaranalytics limit 10;
+-----------------------------------------------------------+
| STR_TO_DATE(JSON_EXTRACT(data, "$.new_time") ,"%Y-%m-%d") |
+-----------------------------------------------------------+
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
+-----------------------------------------------------------+
同样,作为 DATETIME 的 CAST 也会失败:
mysql> select CAST(JSON_EXTRACT(data, "$.new_time") as DATETIME) from analytics_calendaranalytics limit 10;
+----------------------------------------------------+
| CAST(JSON_EXTRACT(data, "$.new_time") as DATETIME) |
+----------------------------------------------------+
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
+----------------------------------------------------+
当我从字符串值开始时,这两个命令都有效:
mysql> select CAST("2016-09-30T04:00:00+00:00" as DATETIME);
+-----------------------------------------------+
| CAST("2016-09-30T04:00:00+00:00" as DATETIME) |
+-----------------------------------------------+
| 2016-09-30 04:00:00 |
+-----------------------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> select STR_TO_DATE("2016-09-30T04:00:00+00:00", "%Y-%m-%d");
+------------------------------------------------------+
| STR_TO_DATE("2016-09-30T04:00:00+00:00", "%Y-%m-%d") |
+------------------------------------------------------+
| 2016-09-30 |
+------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
将不胜感激任何帮助解决这个问题!