Markum
Markum

Reputation: 4049

How do I copy the value of one column into another in the same row?

This is my example table:

+-----+----------+------+
| id  | current  | max  |
+-----+----------+------+
| 1   | 20       | 100  |
| 2   | 50       | 50   |
+-----+----------+------+

I am in need of a query that will set the value of a row's current column to its max column, but I can't find a way to copy it over. This is my current query:

UPDATE `table` SET `current` = ??? WHERE `id` = 1

What would I replace ??? with to use that column's value of 100?

Upvotes: 0

Views: 787

Answers (2)

user756519
user756519

Reputation:

To update the column current with value in max column for only the row id 1, use this script.

UPDATE table 
SET    current = max
WHERE  id = 1;

To update column current with value in max column for all the rows in the table, remove the WHERE condition.

UPDATE table 
SET    current = max;

Upvotes: 2

Oded
Oded

Reputation: 499002

Just set it to that column value, using the column name:

UPDATE `table` SET `current` = `max` WHERE `id` = 1

Upvotes: 3

Related Questions