Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to decrement a variable in MySQL?

Tags:

php

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?

like image 390
kylex Avatar asked Dec 27 '08 22:12

kylex


People also ask

How do I overwrite a record in MySQL?

If you want to OVERWRITE always, (not update, just overwrite) you can use REPLACE instead of INSERT . WARNING: DO NOT use REPLACE if there are other tables with foreign key indexes referring to the table you are updating.


1 Answers

UPDATE forum SET replys = reply - 1 WHERE fid = '$id'
like image 72
stu Avatar answered Sep 30 '22 01:09

stu