Unix Technical Forum

Copy a row into the same table

This is a discussion on Copy a row into the same table within the MySQL forums, part of the Database Server Software category; --> Hi all, is there any way to do the following in SQL: Create a new row by copying an ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-13-2008, 01:59 PM
yataaa
 
Posts: n/a
Default Copy a row into the same table

Hi all,

is there any way to do the following in SQL:
Create a new row by copying an existing row, in such a way that certain
values of the existing row have been changed in the newly created row?
What I mean is "create a new row, use row X as template, but change the
value in column 'name' to 'foobar'"

I tried to do it via a derived table in which I update that column, but
SQL alerted me that this is not possible:
UPDATE
(SELECT * FROM tablename WHERE ID=X) as a
SET name=foobar

There has to be some way to do this, but I just don't get it.
--
kind regards, Dan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-13-2008, 01:59 PM
Dan Rumney
 
Posts: n/a
Default Re: Copy a row into the same table

yataaa wrote:
> Hi all,
>
> is there any way to do the following in SQL:
> Create a new row by copying an existing row, in such a way that certain
> values of the existing row have been changed in the newly created row?
> What I mean is "create a new row, use row X as template, but change the
> value in column 'name' to 'foobar'"
>
> I tried to do it via a derived table in which I update that column, but
> SQL alerted me that this is not possible:
> UPDATE
> (SELECT * FROM tablename WHERE ID=X) as a
> SET name=foobar
>
> There has to be some way to do this, but I just don't get it.


If you used an intermediary table, you could us INSERT...SELECT

http://dev.mysql.com/doc/refman/5.1/...rt-select.html

Copy the row(s) into your intermediary table and then copy them back
with the relevant changes
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-13-2008, 01:59 PM
Dan Rumney
 
Posts: n/a
Default Re: Copy a row into the same table

Dan Rumney wrote:
[snip]
>>
>> There has to be some way to do this, but I just don't get it.

>
> If you used an intermediary table, you could us INSERT...SELECT
>
> http://dev.mysql.com/doc/refman/5.1/...rt-select.html
>
> Copy the row(s) into your intermediary table and then copy them back
> with the relevant changes


Correction.

The documentation contradicts itself as to whether you can use
INSERT...SELECT to INSERT into the same table that you're SELECTing from.

Read the documentation and try it out!

Good luck
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-13-2008, 01:59 PM
Captain Paralytic
 
Posts: n/a
Default Re: Copy a row into the same table

On 12 Jun, 00:10, yataaa <gates...@gmail.com> wrote:
> Hi all,
>
> is there any way to do the following in SQL:
> Create a new row by copying an existing row, in such a way that certain
> values of the existing row have been changed in the newly created row?
> What I mean is "create a new row, use row X as template, but change the
> value in column 'name' to 'foobar'"
>
> I tried to do it via a derived table in which I update that column, but
> SQL alerted me that this is not possible:
> UPDATE
> (SELECT * FROM tablename WHERE ID=X) as a
> SET name=foobar
>
> There has to be some way to do this, but I just don't get it.
> --
> kind regards, Dan


You would do it like this:
Suppose you have a table with 3 fields: field_1, field_2, field_3 and
field_1 is the PK.
You want to copy a record where field_1 = 5, field_2 = 'Fred', field_3
= 'Bloggs' such that the new record has values field_1 = 8, field_2 =
'Fred', field_3 = 'Bloggs'

INSERT INTO table
SELECT
8,
field_2,
field_3
FROM table
WHERE field_1 = 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-13-2008, 02:00 PM
yataaa
 
Posts: n/a
Default Re: Copy a row into the same table

Captain Paralytic wrote:

> INSERT INTO table
> SELECT
> 8,
> field_2,
> field_3
> FROM table
> WHERE field_1 = 5


Ah, Eureka!

Alas.. SQL reports that the ID of the copied row is used twice - I did
not follow your example to set the ID and leave the rest. Is there any
way to let SQL decide what ID to use next?

I know I can work around that problem by selecting MAX(id) and
incrementing it by one, but it does not seem to me like a "nice" solution.

--
Dan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 06-13-2008, 02:00 PM
Rik Wasmus
 
Posts: n/a
Default Re: Copy a row into the same table

yataaa wrote:
> Captain Paralytic wrote:
>
>> INSERT INTO table
>> SELECT
>> 8,
>> field_2,
>> field_3
>> FROM table
>> WHERE field_1 = 5

>
> Ah, Eureka!
>
> Alas.. SQL reports that the ID of the copied row is used twice - I did
> not follow your example to set the ID and leave the rest. Is there any
> way to let SQL decide what ID to use next?
>
> I know I can work around that problem by selecting MAX(id) and
> incrementing it by one, but it does not seem to me like a "nice" solution.


Trying to assign a unique ID has all kinds of consequences (race
conditions etc.).

If your ID as an auto-incrementing field, you can either leave the ID
field out of it:
TABLE:
id
field1
field2

INSERT INTO table (field1,field2) SELECT field1,field2 FROM table;

Or, as with inserts, you can assign a NULL:
INSERT INTO table (id, field1,field2)
SELECT null,field1,field2 FROM table;
Or
INSERT INTO table SELECT null,field1,field2 FROM table;

.... although it's always better to name ones fields explicitly. It is a
little more work earlier one, which will save you a lot of headaches /
bughunts later on if you happen to change the tables.
--
Rik Wasmus
....spamrun finished
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:31 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com