tags:

views:

55

answers:

2

Hi,

I have a table with two columns

id_test1      id_test2  
1             Null  
2             Null  
3             Null  
4             Null  
5             Null  

How can I update or populate the id_test2 as below?

id_test1      id_test2  
1             256  
2             214  
3             147  
4             987  
5             561  

Thanks for any tips

+2  A: 
update myTable set id_test2 = 256 where id_test1 = 1
update myTable set id_test2 = 214 where id_test1 = 2

etc

edit:

Based on your comment, I'd just blow away the existing rows that contain the null values and insert new ones...

delete myTable
insert into myTable (id_test1,id_test2) values (1,256)
insert into myTable (id_test1,id_test2) values (1,214)
...
insert into myTable (id_test1,id_test2) values (2,256)
insert into myTable (id_test1,id_test2) values (2,214)

etc

kekekela
Ooops I forgot one thing. Each id_test1 need to update as follow id_test1 id_test2 1 256 1 214 1 147 1 987 1 561 Thanks.
Oops! I forgot one thing. It supposes to be the following for each id_test1 id_test1 id_test2 1 256 1 214 1 147 1 987 1 561 Thanks
Are there any other ways to do in batch?
I have several hundred rows need to update.
kekekela, you may want to edit your "wipe out" SQL statements. Instead of your delete statement, it should be DROP TABLE myTable
jlafay
The syntax I've given is correct. I'm not trying to drop the table, just delete the rows.
kekekela
+2  A: 
UPDATE test_table 
SET id_test2 = 256
WHERE id_test1 = 1

You didn't include the name of your table so I used test_table instead. This can be used for each record and is pretty SQL agnostic for the most part, meaning the syntax SHOULD work for any RDBMS.

jlafay