解析作为 json 对象的列

Parse a column that is an json object

我想查询此列是一个 JSON 对象。

|  x  |     y     |                        z                  |
----------------------------------------------------------------
|  1  | first      | {"first_name":["Pat","Amy T."],"last_name":["Liman","Pedano"],"updated_at":["1989-11-30T19:00:00.000-05:00","2018-10-11T12:45:44.674-04:00"]}    |
|  2  | second    | {"first_name":["Elli","Ala"],"last_name":["Sheppard","Mcgrial"],"updated_at":["2019-03-16T11:15:25.714-04:00","2019-05-21T11:29:00.684-04:00"]}   |

其中 z 是一个 json 对象。

我试过了

SELECT x, y, t1.first_name, t1.last_name, t1.updated_at
FROM versions_table v
    LATERAL VIEW json_tuple(v.z, 'first_name', 'last_name', 'updated_at') t1 
    as `first_name`, `last_name`, `updated_at`
where first_name is not null;

但我得到了一个 json 数组。更改

的日期格式的最佳方法是什么

输出是这样的2019-01-03

谢谢!

您的日期字符串采用 "combined timestamp" 格式。据我了解,您只是想将该字符串转换为日期类型。

PostgreSQL

以下示例有效,会将您的字符串转换为您想要的日期:

SELECT '2018-10-11T12:45:44.674-04:00'::date

您可以在 this tutorial 中了解有关转换的更多信息。 :: 是转换运算符。

另一种选择是使用 to_date function or similar:

SELECT to_date('2018-10-11T12:45:44.674-04:00', 'YYYY-MM-DD');

MySQL

在 MySQL 中您可以使用 DATE function

SELECT DATE('2018-10-11T12:45:44.674-04:00')