2009-09-27 11 views

回答

1

它可能不是最高效的,但你可以這樣做:

UPDATE my_table 
SET my_date = CONCAT(my_year, '-', my_month, '-', my_day, ' ', 
    my_hour, ':', my_mins, ':', my_seconds) 
WHERE ... 
+0

我需要用雙引號(「),而不是單引號('),但這個工作 – Germstorm 2009-09-27 17:57:37

1

你可以把他們都轉換成字符串,然後將該字符串轉換爲datetime,像這樣:

update tbl 
set datecol = cast(cast(`year` as varchar) + '-' + 
       cast(`month` as varchar) + '-' + 
       cast(`day` as varchar) + ' ' + 
       cast(`hour` as varchar) + ':' + 
       cast(`minute` as varchar) + ':' + 
       cast(`second` as varchar) as datetime) 

雖然,我認爲MySQL使用concat,所以這將是:

cast(concat(cast(`year` as varchar), '-', cast(`month` as varchar), '-',...) as datetime) 
0

您可以通過使用STR_TO_DATE有了它,例如使用CONCAT您的數據一起做東西,看起來像一個DATETIME值

CONCAT(year,'-', month, '-', day,' ',hour, ':', min, ':', sec) 

雖然你可以直接使用,你也可以變成一個真正的DATETIME

STR_TO_DATE(
    CONCAT(year,'-', month, '-', day,' ',hour, ':', min, ':', sec), 
    '%Y-%m-%d %H:%i:%S'); 
0
CREATE TABLE `x` (
    `y` int(4) DEFAULT NULL, 
    `m` int(4) DEFAULT NULL, 
    `d` int(4) DEFAULT NULL, 
    `h` int(4) DEFAULT NULL, 
    `i` int(4) DEFAULT NULL, 
    `s` int(4) DEFAULT NULL 
); 
INSERT INTO x SELECT 2009, 9, 1, 9, 59, 59; 
SELECT STR_TO_DATE(CONCAT(y,'-', m,'-', d,'-', h,'-', i,'-', s), '%Y-%m-%d-%h-%i-%s') FROM x;