PDA

View Full Version : SQL query question - Copy a column from one table to another


cinq
02-05-2005, 12:16 PM
How can this be done ?

Background:
One table, articles_article has already been created, with rows having the first column articles_articleid, being the unique id for each row.

I want to create another table, articles_newtable with exactly the same number of rows with each row having the same corresponding articles_articleid.

How can this be achieved ?
Thanks in advance :)

Xenon
02-05-2005, 12:23 PM
that way should work:


INSERT INTO articles_newtable (articleid)
SELECT articles_articleid FROM articles_article

so you see you can mix up insert an select queries :)

cinq
02-05-2005, 12:25 PM
I never knew that ! Thanks Stefan :D

Xenon
02-05-2005, 12:29 PM
you're welcome

Michael Morris
02-05-2005, 03:22 PM
A similar technique can be used to copy table contents over within or even between databases.

create table destinationdatabase.newtable
select * from sourcedatabase.oldtable

You don't have to specificy databases if making a transfer within the same database.

Xenon
02-06-2005, 07:40 PM
hey, that one was even new to me :)

you see you can learn more every day..

Dean C
02-06-2005, 07:45 PM
This is an enlightening thread :)