MySQL value minus 1?

Post here if you need help with SQL.
Post Reply
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

MySQL value minus 1?

Post 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 :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: MySQL value minus 1?

Post 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]
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: MySQL value minus 1?

Post by Helx »

Oh XD

I didn't realise that you could use maths operators in SQL statements :/ probably should have googled that first :P
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: MySQL value minus 1?

Post 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.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
Post Reply