Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy text column value from one row to another row in mySQL

I have table pref having column value. This value has type text. I want copy the value field value of row with id 7 to the value field of row with id 1. Can you please help how to do this. I know MS SQL, but I am new to mySQL.

create table pref
(
   id int,
   value text
)
like image 938
dotcoder Avatar asked Jan 21 '11 07:01

dotcoder


2 Answers

In MySQL you can't use a subselect from the same table you are updating, but you can use a join.

   UPDATE pref AS target
LEFT JOIN pref AS source ON source.id = 7
      SET target.value = source.value
    WHERE target.id = 1;
like image 108
Phssthpok Avatar answered Oct 12 '22 18:10

Phssthpok


UPDATE
    pref
SET
    value = (SELECT value WHERE id = 7)
WHERE
    id = 1
like image 33
Fidi Avatar answered Oct 12 '22 19:10

Fidi