mysql from \u unixtime()返回null,最后一个\u update列为sakila.actor

w1e3prcc  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(471)

salika示例模式中的actor表将列last\u update定义为时间戳。我想用iso8601格式的json\ U数组呈现该列。-首先,这不应该是json\u数组的默认呈现。
从阅读本网站和其他网站上的文档和评论来看,答案似乎是使用from\u unxtime生成iso8601格式的输出掩码。
不幸的是,from\u unixtime()在我的数据库中似乎总是返回null

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2018-10-03 17:15:03 |
+---------------------+
1 row in set (0.00 sec)

mysql> select from_unixTime(current_timestamp())
-> ;
+------------------------------------+
| from_unixTime(current_timestamp()) |
+------------------------------------+
| NULL                               |
+------------------------------------+
1 row in set (0.00 sec)

mysql>

我怀疑这可能是因为我没有安装时区配置文件。。然而,当我尝试,我得到。。。

mysql -u root -p******sys  <timezone_posix.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'sys.time_zone' doesn't exist

当然我错过了一些明显的东西。。。。

p3rjfoxz

p3rjfoxz1#

这最终对我有用。。。

select json_array("actor_id","first_name","last_name",DATE_FORMAT(convert_tz("last_update", @@session.time_zone, '+00:00'),'%Y-%m-%dT%T%fZ')) "json" from "sakila"."actor"

/
这给了

[
            1,
            "PENELOPE",
            "GUINESS",
            "2006-02-15T12:34:33000000Z"
        ],
        [
            2,
            "NICK",
            "WAHLBERG",
            "2006-02-15T12:34:33000000Z"
        ]
gab6jxml

gab6jxml2#

试试这个:

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_TIMESTAMP()));

正如mysql doc所说,函数 FROM_UNIXTIME(unix_timestamp[,format]) 仅接受参数作为 UNIX_TIMESTAMP ,但不是 TIMESTAMP .
幸运的是,有一个函数 UNIX_TIMESTAMP(date) 转换各种类型,例如 DATE , TIMESTAMP 以此类推 UNIX_TIMESTAMP . 所以打电话 UNIX_TIMESTAMP(date) 先是,然后是“从\u unixtime”(

相关问题