0

我有以下触发器,它在表 A 更新时将记录插入表 B。这很好用,但是TableA_date是 unix 时间格式,我想在触发器将记录插入表 B 时对其进行转换。

DELIMITER $$
CREATE TRIGGER MyTrigger
AFTER INSERT
ON TableA
FOR EACH ROW 
BEGIN
    INSERT INTO TableB SET 
    TableB_id = NEW.TableA_id, 
    TableB_date = FROM_UNIXTIME(NEW.TableA_date, '%d/%m/%y %r'),
    TableB_comment = NEW.TableA_comment;
END $$
DELIMITER ;

在我的结果中,我得到的转换日期不是“01/01/70 03:00:05 AM”,而是“5”——我知道格式字符串是正确的,因为我可以在选择语句中使用它。谢谢你的帮助

4

1 回答 1

0

You are working too hard. Just leave out the format string entirely. Dates are stored as dates, not a strings.

In fact, don't even use the conversion at all! Just directly assign the date column, and MySQL will do any conversion it needs.

I'm assuming that TableA_date is stored using the timestamp datatype, and that TableB_date is datetime. (Although you may want timestamp for that one too.)

If you are using other datatypes like int, or char, then fix it. That's not the correct way to structure a database.


Just to answer your actual question (instead of telling you the correct way to do it) the date format you want is yyyy-mm-dd hh:mm:ss not what you have.

于 2012-09-04T22:32:05.817 回答