MySQL: Insert datetime into other datetime field

后端 未结 4 1063
误落风尘
误落风尘 2020-12-02 20:24

I have a table with a DATETIME column. I would like to SELECT this datetime value and INSERT it into another column.

I did this (note: \'2011-12-18 13:17:17\' is the

相关标签:
4条回答
  • 2020-12-02 20:26

    According to MySQL documentation, you should be able to just enclose that datetime string in single quotes, ('YYYY-MM-DD HH:MM:SS') and it should work. Look here: Date and Time Literals

    So, in your case, the command should be as follows:

    UPDATE products SET former_date='2011-12-18 13:17:17' WHERE id=1
    
    0 讨论(0)
  • 2020-12-02 20:40

    for MYSQL try this

    INSERT INTO table1(myDatetimeField)VALUES(STR_TO_DATE('12-01-2014 00:00:00','%m-%d-%Y %H:%i:%s');

    verification-

    select * from table1
    output- datetime= 2014-12-01 00:00:00

    0 讨论(0)
  • 2020-12-02 20:46

    Try

        UPDATE products SET former_date=20111218131717 WHERE id=1
    

    Alternatively, you might want to look at using the STR_TO_DATE (see STR_TO_DATE(str,format)) function.

    0 讨论(0)
  • 2020-12-02 20:52

    If you don't need the DATETIME value in the rest of your code, it'd be more efficient, simple and secure to use an UPDATE query with a sub-select, something like

    UPDATE products SET t=(SELECT f FROM products WHERE id=17) WHERE id=42;
    

    or in case it's in the same row in a single table, just

    UPDATE products SET t=f WHERE id=42;
    
    0 讨论(0)
提交回复
热议问题