2016-01-22 169 views
5

我是新来的postgres,有时我一直在为Mysql工作。我需要将内容从Mysql表迁移到Postgres表。如何将Mysql时间戳转换为sysdate(6)格式(以毫秒为单位)?

我的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 

我插入created_at和的updated_at从MySQL表,该表的形式为“2014年9月4日23时40分十四秒”的价值。

当我向postgres表中插入一行时,默认时间戳的格式为“2016-01-22 17:44:53.342757”,其中包含时间戳记中的毫秒。

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

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

在此先感谢!

+3

也许你需要阅读[时间戳以毫秒presicion如何保存在MySQL(http://stackoverflow.com/questions/26299149/timestamp-with-a-millisecond -precision-how-to-save-them-in-mysql) – eusoj

+0

你需要在mysql中使用这个精度,还是你认为你需要迁移? postgresql也很乐意接受更短的时间戳,即使是“2016-01-22”作为“2016-01-22 00:00:00.000000”的缩写 – knitti

回答

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) 

而且我引用11.3.6 Fractional Seconds in Time Values 哪个版本的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'; 

退出并重新登录,测试

mysql -u test -p 

密码为test

然后

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

INSERT INTO test.td6 VALUES ('2016-01-22 17:44:53.342757'); 

插入的值是你想插入到MySQL时间戳格式。 Example Results Ref msql 5.7

所有最优秀的

相关问题