Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update a column by subtracting a value

Tags:

sql

mysql

I'm trying to come up with a MySQL query that will update points... Can I do something like this?

UPDATE `a75ting`.`username` SET `points` = '`points` - 5' 
like image 247
Shawn Avatar asked Mar 21 '11 20:03

Shawn


People also ask

How do you subtract a value from a column in SQL?

Basic Syntax:columnN FROM table_name WHERE condition MINUS SELECT column1 , column2 , ... columnN FROM table_name WHERE condition; columnN: column1, column2.. are the name of columns of the table.

How do I change the value of a column based on another column?

UPDATE table SET col = new_value WHERE col = old_value AND other_col = some_other_value; UPDATE table SET col = new_value WHERE col = old_value OR other_col = some_other_value; As you can see, you can expand the WHERE clause as much as you'd like in order to filter down the rows for updating to what you need.


1 Answers

UPDATE a75ting.username SET points = points - 5 

by putting the single quotes around the "points -5", you converted that expression into a plaintext string. Leaving it without the quotes lets MySQL see you're referring to a field (points) and subtracting 5 from its current value.

like image 77
Marc B Avatar answered Oct 04 '22 04:10

Marc B