2014-05-08 22 views

回答

0

如果date列的值为int窗体,则它们必须位于unix_timestamp窗体中。

如果要将列转换为datedatetime数据类型,最好添加一个具有所需数据类型的新列。然后更新从旧int列中选择的列。最后,您可以删除int列,并开始使用date类型列。

-- add new datetime type column 
ALTER TABLE table_name 
    ADD COLUMN date_col_new_name DATETIME; 

-- add values into the same column 
-- read from int type date column 
UPDATE table_name 
    SET date_col_new_name = FROM_UNIXTIME(int_type_date_col); 

-- now drop the int type date column 
ALTER TABLE DROP COLUMN int_type_date_col; 

:在UNIX_TIMESTAMPFROM_UNIXTIME

mysql> drop table if exists ut_xmple; 
mysql> create table ut_xmple(date int); 
mysql> insert into ut_xmple(date) values (unix_timestamp()); 
mysql> select * from ut_xmple; 
+------------+ 
| date  | 
+------------+ 
| 1399536302 | 
+------------+ 

mysql> alter table ut_xmple add column date_time datetime; 
mysql> select * from ut_xmple; 
+------------+-----------+ 
| date  | date_time | 
+------------+-----------+ 
| 1399536302 | NULL  | 
+------------+-----------+ 

mysql> update ut_xmple set date_time=from_unixtime(date); 
mysql> select * from ut_xmple; 
+------------+---------------------+ 
| date  | date_time   | 
+------------+---------------------+ 
| 1399536302 | 2014-05-08 13:35:02 | 
+------------+---------------------+ 

mysql> select now(), @ut:=unix_timestamp() ut, from_unixtime(@ut) fut; 
+---------------------+------------+---------------------+ 
| now()    | ut   | fut     | 
+---------------------+------------+---------------------+ 
| 2014-05-08 13:35:54 | 1399536354 | 2014-05-08 13:35:54 | 
+---------------------+------------+---------------------+ 

参见

+0

Hi Ravinder,日期颜色中的值在20,110,113记录中1 20,110,213记录2 20,110,122记录3 20,110,115 - 记录4在表i的数据定义中可以看到日期列的列存储数据类型为int。我只在这个领域保持日期,没有时间。 – user3615156

相关问题