Take the 2-minute tour ×

I've used the SQL command line in phpMyAdmin to update my database directly before (rather than writing a C script). I'm wondering if it's possible to do something a little more complicated.

I have two tables. tableB is currently empty, and exists because I want to change the structure of tableA. (I want to drop snum from tableA.)

unum   (int)
snum   (int)

unum   (int)
snum   (int)

What I need to do is create one record in tableB for each and every record in tableA, copying across unum and snum as I go. Then I will remove snum from tableA and henceforth use a LEFT JOIN to combine the tables. (The problem is that there can be multiple snum values for each unum which wasn't originally foreseen.)

Is it possible to populate tableB just through a MySQL command in phpMyAdmin, or do I need to write a little script?


share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

loop isn't necessary here, you can simply do this since you want to copy rows from tableA to tableB. execute INSERT INTO...SELECT statement,

INSERT INTO TableB (unum, snum)
SELECT unum, snum
FROM tableA

follow-up question: what will be the value for snum since there are many of them on each unum? do you want to insert it all or selected only?

share|improve this answer
Thanks for this. You're my new best friend - two answers in two days :) Regarding your follow-up question, there is only currently one snum for each unum at present. Once I've made the db change, I can add the extra data :) –  Nick Jan 1 '13 at 5:41
@Nick sure, i'll be waiting for that. You're welcome! –  John Woo Jan 1 '13 at 5:43
add comment

Your Answer


