Transfer 1 row from Table A to Table B?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

Transfer 1 row from Table A to Table B?

Post by rxsid »

Hi all,

How could I transfer 1 returned row from table A, into table B without getting each column's data in that row (from A) and then doing an insert statement into table B.

Is there a function or sql language to do this?

Thanks
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

I don't believe there's an automatic way to so this, since it's unusual to have two tables with identical structures that would make it safe to do what you're describing.

You could write a function in PHP that would create an insert statement based on the row data returned from your query. You could then submit this input statement to MySQL. This would avoid having to hard-code each individual column name in your code.
Elena Mitovska
Forum Newbie
Posts: 6
Joined: Mon Nov 18, 2002 3:43 am
Location: Ukraine
Contact:

Post by Elena Mitovska »

YOu can cope data from one table toanother one with the following statement:

INSERT INTO tableB SELECT * FROM tableA WHERE....

Only make sure that amoutn of columns you select from table A exactly match amount and type of coulmns in table B.
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

You're right, Elena. I always forget you can do that, probably because I'm always nervous to assume that both tables have matching structures. Thanks for correcting me.
Post Reply