17

我正在尝试从 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)

将不胜感激任何帮助解决这个问题!

4

2 回答 2

31

你必须使用JSON_UNQUOTE

select CAST( JSON_UNQUOTE( JSON_EXTRACT(data, "$.new_time")) as DATETIME) from analytics_calendaranalytics limit 10;

会工作。我说会因为你没有提供样本数据。我尝试如下:

select @js := JSON_OBJECT('new_time',"2016-09-30T04:00:00+00:00"  );

select CAST(JSON_UNQUOTE(JSON_EXTRACT(@js,'$.new_time')) as DATETIME);

以下查询也有效

 select STR_TO_DATE(JSON_UNQUOTE(JSON_EXTRACT(@js,'$.new_time')) ,"%Y-%m-%d");
于 2016-09-28T09:15:22.220 回答
2

易读的方法是使用->>

SELECT CAST(data->>'$.new_time' AS DATETIME) FROM analytics limit 10;

例子:

mysql> CREATE TABLE analytics (data json not null);
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO analytics (data) VALUES ('{"new_time": "2021-04-21T04:00:00+00:00"}');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT CAST(data->>'$.new_time' AS DATETIME) FROM analytics limit 10;
+---------------------------------------+
| CAST(data->>'$.new_time' AS DATETIME) |
+---------------------------------------+
| 2021-04-21 04:00:00                   |
+---------------------------------------+
1 row in set, 1 warning (0.00 sec)
于 2021-04-21T15:09:48.100 回答