如何将Mysql时间戳转换为sysdate(6)格式(毫秒)?

Aru*_*raj 5 mysql postgresql timestamp

我是postgres的新手,有时候我一直在研究Mysql.我需要将内容从Mysql表迁移到Postgres表.

我的Postgres表是这样的:

             Column             |            Type             |                                           Modifiers                                            
--------------------------------+-----------------------------+------------------------------------------------------------------------------------------------
 id                             | integer                     | 
 created_at                     | timestamp without time zone | not null default timezone('UTC'::text, now())
 updated_at                     | timestamp without time zone | not null default timezone('UTC'::text, now())
 key_classification             | character varying(2000)     | not null
Run Code Online (Sandbox Code Playgroud)

我从mysql表中插入created_at和updated_at值,格式为"2014-09-04 23:40:14".

当我在postgres表中插入一行时,默认时间戳的格式为"2016-01-22 17:44:53.342757",其中包括时间戳中的毫秒数.

现在我需要将毫秒添加到mysql时间戳格式以匹配postgres表格式.

请帮助将毫秒添加到时间戳中.

提前致谢!

小智 0

早上好,Arunraj 我希望这有帮助吗?

我正在运行 Mysql/MariaDB

MariaDB [(none)]> SHOW VARIABLES LIKE "%version%";
+-------------------------+-----------------+
| Variable_name           | Value           |
+-------------------------+-----------------+
| innodb_version          | 5.6.25-73.1     |
| protocol_version        | 10              |
| slave_type_conversions  |                 |
| version                 | 10.0.21-MariaDB |
| version_comment         | MariaDB Server  |
| version_compile_machine | x86_64          |
| version_compile_os      | Linux           |
| version_malloc_library  | system          |
+-------------------------+-----------------+
8 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

我引用的是11.3.6 时间值中的小数秒, 这是 mysql 版本 5.7

运行该示例

As mysql Admin 
mysql -u USERNAME -p

CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';

CREATE DATABASE test;

GRANT ALL ON test.* TO 'test'@'localhost';
Run Code Online (Sandbox Code Playgroud)

退出然后以测试身份登录

mysql -u test -p
Run Code Online (Sandbox Code Playgroud)

密码是测试

然后

CREATE TABLE test.td6(ts6 timestamp(6));

INSERT INTO test.td6 VALUES ('2016-01-22 17:44:53.342757');
Run Code Online (Sandbox Code Playgroud)

插入的值是您要插入到 mysql 中的时间戳格式。 示例结果参考 msql 5.7

一切顺利