Page 1 of 1

MySQL value minus 1?

Posted: Fri Sep 21, 2012 11:04 am
by Helx
Hi there,

I've been having (more) troubles with SQL :/

I need to get an SQL value (the column is an int) and then minus one from that.
The only problem is, it won't do anything :/ (no errors)

[syntax=php]$q = mysql_query("SELECT * FROM stats WHERE `user` = '{$session_NAME}'");
$q = mysql_fetch_array($q);
$total_stats = $q['total_stats']-1;

mysql_query("UPDATE stats SET `total_stats` = '{$total_stats}' WHERE `user` = '{$session_NAME}'");[/syntax]

I really can't see whats wrong here :<
(And yes, this is for a statistics script. I would like to be able to delete entries)

If anyone could help, that would be amazing :)

Re: MySQL value minus 1?

Posted: Sat Sep 22, 2012 9:48 pm
by jacek
If you don't need the value in the script afterwards you can do it in one query.

[syntax=sql]UPDATE `table` SET `value` = `value - 1 WHERE `id` = 42[/syntax]

Re: MySQL value minus 1?

Posted: Sat Sep 22, 2012 11:31 pm
by Helx
Oh XD

I didn't realise that you could use maths operators in SQL statements :/ probably should have googled that first :P

Re: MySQL value minus 1?

Posted: Mon Sep 24, 2012 6:52 am
by bowersbros
SQL is quite a powerful language. You can nest statements, create procedures (basically functions) and do all sorts of useful things. it is far more advanced than just Select, insert, delete or update.