JuanBonnett
JuanBonnett

Reputation: 786

MYSQL Update all values with the value of an specific row

How can I achieve something like this:

UPDATE products SET description = (( description of the row id 15 ))

It means all rows will have the same description of the product number 15.

Upvotes: 0

Views: 24

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

In MySQL, use a join:

UPDATE products p cross join
       (select description
        from products
        where rowid = 15
       ) p15
   SET p.description = p15.description;

EDIT:

The subquery isn't necessary. I just think it makes the query more readable. It should have minimal impact on performance, because presumably it only returns one row. You could write this as:

UPDATE products p join
       products p15
       on p15.rowid = 15
   SET p.description = p15.description;

Upvotes: 1

Related Questions