2014-05-08 27 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

相關問題