Page 1 of 1

simple question that I cant solved

Posted: Tue Dec 29, 2009 9:33 am
by kingdm
Hello.

I've got this question. I have a database with one table 'users'. Under these is a lot of columns, with almost 200+ records(rows).

Now my problem is I added a new field 'elem_course' and 'high_course'. Of course it's empty. Now I want all my records to have the value of "Primary" for the 'elem_course' and "Secondary" for the 'high_course'?

How can I do this in SQL or PHP? I'm asking because I'm tired of editing all of them one by one in phpmyadmin of XAMPP

Thanks. :banghead:

Re: simple question that I cant solved

Posted: Tue Dec 29, 2009 9:37 am
by JNettles
When you create a new column you can specify a default value that updates all existing rows. Look for the option in PHPmyadmin or you can use sql that looks something like....

UPDATE users SET elem_course = 'Primary';

Double check that - should work but my mind is thinking in DB2 right now.....

Re: simple question that I cant solved

Posted: Tue Dec 29, 2009 10:03 am
by kingdm
JNettles wrote:When you create a new column you can specify a default value that updates all existing rows. Look for the option in PHPmyadmin or you can use sql that looks something like....

UPDATE users SET elem_course = 'Primary';

Double check that - should work but my mind is thinking in DB2 right now.....
Thanks for your input mate. It did makes sense in me while trying, problem was solved. Credits also to skywalker from webdeveloper. Here's the query in case someone might have the similar problem.

Code: Select all

UPDATE users
SET 
elem_course = 'Primary',
high_course = 'Secondary'

Re: simple question that I cant solved

Posted: Tue Dec 29, 2009 1:58 pm
by manohoo
One thing to add to JNettles post, be careful with your SQL update, if you don't have a WHERE clause you can overwrite the entire column, that could be a real headache!

Re: simple question that I cant solved

Posted: Tue Dec 29, 2009 2:09 pm
by JNettles
Good point but if his entire column is empty then it won't make any difference. But, as you said, its a good idea to do WHERE blah = '' or something like that.