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

前端 未结 7 802
时光取名叫无心
时光取名叫无心 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:09

    SET @sql = 
    CONCAT( 'INSERT INTO  (', 
        (
            SELECT GROUP_CONCAT( CONCAT('`',COLUMN_NAME,'`') ) 
                FROM information_schema.columns 
                WHERE table_schema = 
                    AND table_name = 
                    AND column_name NOT IN ('id')
        ), ') SELECT ', 
        ( 
            SELECT GROUP_CONCAT(CONCAT('`',COLUMN_NAME,'`')) 
            FROM information_schema.columns 
            WHERE table_schema = 
                AND table_name = 
                AND column_name NOT IN ('id')  
        ),' from  WHERE  = ' );  
    
    PREPARE stmt1 FROM @sql; 
    execute stmt1;
    

    Of course replace <> values with real values, and watch your quotes.

提交回复
热议问题