Welcome to WebmasterWorld Guest from 54.166.46.226

Forum Moderators: open

Message Too Old, No Replies

Copy Certain Fields To A New Row In MySQL?

   
3:53 pm on Mar 22, 2012 (gmt 0)

WebmasterWorld Senior Member planet13 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Hi there, Everyone:

I would like to copy certain fields to a NEW ROW in the same table in a MySQL database.

For example, the structure Might look like this:

ID (primary key) | SKU | TYPE | VALUE
0001 | ABC321 | phone_number | 555-1212

I would like to COPY the SKU field (ABC321) and the VALUE field (555-1212) but would like to have a NEW value for the TYPE field (change it from phone_number to custom_phone).

And of course, the ID (which is a primary key) should be populated automatically.

So in my example, I would like to take this data:

0001 | ABC321 | phone_number | 555-1212

And copy it into:

0002 | ABC321 | custom_phone | 555-1212


Also, how can I use ignore so that it doesn't create a duplicate row if one already exists?

Thanks in advance.
10:38 pm on Mar 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you want is something like

INSERT INTO 'table' (list of columns to insert)
SELECT list of columns/values
FROM 'table'
WHERE condition of row to copy
AND NOT EXISTS ( SELECT to check to see if row already exists )
11:10 pm on Mar 27, 2012 (gmt 0)

WebmasterWorld Senior Member planet13 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Thanks so much, Dijkgraaf