INSERT INTO SELECT - Change a value

You can also modify columns at insert by using

INSERT INTO ...() SELECT ...REPLACE(fieldname, 'somestring','anotherstring') ...

Or even use a subquery

INSERT INTO ...() SELECT ...(SELECT id FROM tableName WHERE somefield = 'asd')... 

INSERT INTO table2 (username, volume, name, image, content, cssanimate) 
    SELECT 'tim', volume, name, image, content, cssanimate 
    FROM table1 where volume='Story of a Girl';

If you list the columns you want to insert, you can replace columns with custom values.

You can get more complex too:

INSERT INTO table2 (username, volume, name, image, content, cssanimate) 
    SELECT 
        CASE 
            WHEN 'admin' THEN 'tim' 
            ELSE username 
        END CASE, 
        volume, name, image, content, cssanimate 
    FROM table1;

Tags:

Mysql