Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update a column for all rows in a table with different values for each row in a table of 100 records

my table contains 100 records and i need to update a column with new values(diff value for each record) for each of the 100 records .How can i do this. the column to update the values is not primary key.

like image 891
susie Avatar asked Feb 23 '23 17:02

susie


2 Answers

UPDATE tablename
   SET columnname = CASE id
                        WHEN 1 THEN 42
                        WHEN 2 THEN 666
                    END

With this query columnname will be updated to 42 for the row with id = 1, and 666 for id = 2

like image 193
zerkms Avatar answered Feb 26 '23 10:02

zerkms


Create a table with an autoicrement id and the columns of the original table.

Then

INSERT INTO new_table (column1, column2,.....) -- refer all columns except autoincrement id
SELECT * FROM old_table

Update the old table by joining with the new, assuming the is a key composite or not that distincts each row

like image 31
niktrs Avatar answered Feb 26 '23 11:02

niktrs