MYSQL: How to copy an entire row from one table to another in mysql with the second table having one extra column?

前端 未结 7 796
时光取名叫无心
时光取名叫无心 2020-12-12 23:48

I have two tables with identical structure except for one column... Table 2 has that additional column in which i would insert the CURRENT_DATE()

I would like to cop

相关标签:
7条回答
  • 2020-12-13 00:11

    The safest way to do it is to fully specify the columns both for insertion and extraction. There's no guarantee (to the application) that either of these will be the order you think they may be.

    insert into dues_storage (f1, f2, f3, cd)
        select f1, f2, f3, current_date() from dues where id = 5;
    

    If you're worried about having to change many multiple PHP pages that do this (as you seem to indicate in the comment to another answer), this is ripe for a stored procedure. That way, all your PHP pages simply call the stored procedure with (for example) just the ID to copy and it controls the actual copy process. That way, there's only one place where you need to maintain the code, and, in my opinion, the DBMS is the right place to do it.

    0 讨论(0)
提交回复
热议问题