Unix Technical Forum

Moving row to different table, and changing value at same time

This is a discussion on Moving row to different table, and changing value at same time within the MySQL forums, part of the Database Server Software category; --> I really don't have a "test" table to try this with, and I thought that it might be better ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:22 AM
Jason
 
Posts: n/a
Default Moving row to different table, and changing value at same time

I really don't have a "test" table to try this with, and I thought
that it might be better to just ask, anyway, since there might be a
better option.

I have 2 tables with the same columns. I want to move a row from Table
A to Table B:

INSERT INTO table_b SELECT * FROM table_a WHERE id=$id

However, it's likely that $id already exists in Table B, so I need it
to FIND the row by the ID column, but then at the same time change it
to something that I specify when it writes it to Table A.

So, assuming that $id=477, but in the new table I want it to be 17238.
Is this the correct command?

INSERT INTO table_b SELECT * FROM table_a WHERE id=477 SET id=17238

Either way, is there a better/more efficient solution than this?

TIA,

Jason

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:22 AM
Rik
 
Posts: n/a
Default Re: Moving row to different table, and changing value at same time

On Mon, 06 Aug 2007 02:05:08 +0200, Jason <jwcarlton@gmail.com> wrote:

> I really don't have a "test" table to try this with,


Well, make one

> and I thought
> that it might be better to just ask, anyway, since there might be a
> better option.
>
> I have 2 tables with the same columns. I want to move a row from Table
> A to Table B:
>
> INSERT INTO table_b SELECT * FROM table_a WHERE id=$id
>
> However, it's likely that $id already exists in Table B, so I need it
> to FIND the row by the ID column, but then at the same time change it
> to something that I specify when it writes it to Table A.
>
> So, assuming that $id=477, but in the new table I want it to be 17238.


What is the logic behing 17238?

> Is this the correct command?
>
> INSERT INTO table_b SELECT * FROM table_a WHERE id=477 SET id=17238
>
> Either way, is there a better/more efficient solution than this?


You might use a LEFT JOIN to table_b in the SELECT statement, and set the
id according to wether it has a match in table_b

Then again: what is the point of id's if they change? If it's for
backwards compatibility, I'd just make a table_c with an real new id
field, and some old_id, and fill it with table_a & table_b both....
--
Rik Wasmus
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:22 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Moving row to different table, and changing value at same time

Jason wrote:
> I really don't have a "test" table to try this with, and I thought
> that it might be better to just ask, anyway, since there might be a
> better option.
>
> I have 2 tables with the same columns. I want to move a row from Table
> A to Table B:
>
> INSERT INTO table_b SELECT * FROM table_a WHERE id=$id
>
> However, it's likely that $id already exists in Table B, so I need it
> to FIND the row by the ID column, but then at the same time change it
> to something that I specify when it writes it to Table A.
>
> So, assuming that $id=477, but in the new table I want it to be 17238.
> Is this the correct command?
>
> INSERT INTO table_b SELECT * FROM table_a WHERE id=477 SET id=17238
>
> Either way, is there a better/more efficient solution than this?
>
> TIA,
>
> Jason
>


INSERT INTO table_b (id, cola, colb, colc, cold) SELECT 17238m cola,
colb, colc, cold FROM table_a where id=477

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:22 AM
Jason
 
Posts: n/a
Default Re: Moving row to different table, and changing value at same time

> What is the logic behing 17238?
<snip>
> Then again: what is the point of id's if they change? If it's for
> backwards compatibility, I'd just make a table_c with an real new id
> field, and some old_id, and fill it with table_a & table_b both....


I had an old format using flat text files, and it worked well at the
time by having the text files in different directories. I wrote that
program about 7 years ago, though, when I had about 20 users a week.
Now, I have closer to 50,000 users a week, which is why I've moved it
to MySQL, and at the time didn't take this in to consideration.

Now, I'm sort of stuck using separate tables for different boards, so
that old links will continue to work.

Thanks for the tip,

Jason

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:22 AM
Jason
 
Posts: n/a
Default Re: Moving row to different table, and changing value at same time

> INSERT INTO table_b (id, cola, colb, colc, cold) SELECT 17238m cola,
> colb, colc, cold FROM table_a where id=477


Just so that I can understand this...

When you wrote SELECT 17238m, was the "m" a typo, or does it have a
special meaning that I'm not aware of?

TIA,

Jason

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 11:22 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Moving row to different table, and changing value at same time

Jason wrote:
>> INSERT INTO table_b (id, cola, colb, colc, cold) SELECT 17238m cola,
>> colb, colc, cold FROM table_a where id=477

>
> Just so that I can understand this...
>
> When you wrote SELECT 17238m, was the "m" a typo, or does it have a
> special meaning that I'm not aware of?
>
> TIA,
>
> Jason
>


A typo - should be a comma (17238,)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
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:46 PM.


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