kylex
kylex

Reputation: 14406

Is it possible to decrement a variable in MySQL?

I'm wanting to decrement a variable in a MySQL table by one everytime an UPDATE query is ran.

What I have is this, which isn't working:

UPDATE forum SET replys = reply-- WHERE fid = '$id'

Is this possible in any way, or am I going to have to run a SELECT and get the value first, decrement it, and then insert the new value into the UPDATE query?

Upvotes: 5

Views: 8437

Answers (2)

stu
stu

Reputation: 8805

UPDATE forum SET replys = reply - 1 WHERE fid = '$id'

Upvotes: 17

duffymo
duffymo

Reputation: 308763

of course:

UPDATE forum SET replies=replies-1 WHERE fid = ?

Upvotes: 6

Related Questions