Demonic Posted February 26, 2008 Share Posted February 26, 2008 Wondering if you can do math with SQL: mysql_query("UPDATE `member` SET `cash` += 1 WHERE `id` = NUM "); Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 You can do math but not like that. This would be correct: mysql_query("UPDATE `member` SET `cash` = `cash` + 1 WHERE `id` = NUM "); also: SELECT * FROM member WHERE ((fieldA + fieldB) - fieldC) > fieldD) ... just an example.... Quote Link to comment Share on other sites More sharing options...
Demonic Posted February 26, 2008 Author Share Posted February 26, 2008 Is there some way of checking if a result isn't equal to zero at the same time while im updating the row? $num = somebignumber; UPDATE `mem` SET `cash` = (`cash` - $num > 0 ? `cash` - $num : 0) WHERE `id` = 1 Just an example of how I want to do the check Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 Is there some way of checking if a result isn't equal to zero at the same time while im updating the row? $num = somebignumber; UPDATE `mem` SET `cash` = (`cash` - $num > 0 ? `cash` - $num : 0) WHERE `id` = 1 Just an example of how I want to do the check UPDATE `mem` SET `cash` = ( SELECT IF ((`cash` - $num) > 0), (`cash` - $num), 0 ) I guess this should work, haven't tested Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted February 26, 2008 Share Posted February 26, 2008 before you become too deep into writing complicated queries ask your self if I can do this faster in php first, odds are you can do it much quicker in php and save your self the time of writing this complicated if/else strucutred queries. MySQL is great, but when you have the lobster of php sitting there why do you want to take the boring t-bone steak over it (t-bone steak = mysql). yes they all come out the same, but one goes in easier. Or even better use them both (some ppl call this surf and turf) now I'm hungry. Quote Link to comment Share on other sites More sharing options...
Demonic Posted February 26, 2008 Author Share Posted February 26, 2008 $select = sprintf("SELECT * FROM `transactions` WHERE `id` = %d", $_POST['reverse']); $q1 = mysql_query($select) or die(mysql_error()); if( mysql_num_rows($q1) > 0 ) { $f= mysql_fetch_array($q1); $sql[] = sprintf("UPDATE `mem` SET `uU` = (SELECT IF ((`uU` - %d) > 0), (`uU` - %d), 0) WHERE `uid` = %d", $f['sent'],$f['sent'],$f['mem']); $sql[] = sprintf("UPDATE `mem` SET `uM` = (SELECT IF ((`uM` - %d) > 0), (`uM` - %d), 0) WHERE `uid` = %d", $f['sent'],$f['sent'],$f['mem']); for($x=0;$x<sizeof($sql);$x++) { if( !mysql_query($sql[$x]) ) { echo "Something wen't wrong :S $x".mysql_error() . "<br>"; exit; } } echo "success"; } I get an error There was an error: Something wen't wrong :S 0You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '), (`uU` - 1 WHERE `uid` = 38451 at line 1 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted February 26, 2008 Share Posted February 26, 2008 make <?php if( !mysql_query($sql[$x]) ) { echo "Something wen't wrong :S $x".mysql_error() . "<br>"; exit; } ?> to <?php mysql_query($sql[$x]) or die(mysql_error()."<br /><br />".$sql[$x]); ?> SHould be easier to see issues Quote Link to comment Share on other sites More sharing options...
Demonic Posted February 26, 2008 Author Share Posted February 26, 2008 Uh its the same thing..wth.. Anyways anyone know how to use the mysql if statements? I can't quite understand it. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted February 26, 2008 Share Posted February 26, 2008 yes but it reproduces the query which I wanted to look at because the mysql error messages are cut off usually Quote Link to comment Share on other sites More sharing options...
Demonic Posted February 26, 2008 Author Share Posted February 26, 2008 Its really not that hard to read the code and the quote: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '), (`uU` - 1 WHERE `uid` = 38451 at line 1 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted February 26, 2008 Share Posted February 26, 2008 I guess you don't want help then Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 The right syntax should be: UPDATE `mem` SET `uM` = ( SELECT IF ((SELECT (`uM` - %d) FROM `mem` WHERE `uid` = %d) > 0), (SELECT (`uM` - %d) FROM `mem` WHERE `uid` = $d), 0 ) The problem with this is that it won't let you update the same table.... I guess it's because you are reading and writing it at the same time. Quote Link to comment Share on other sites More sharing options...
Demonic Posted February 26, 2008 Author Share Posted February 26, 2008 UPDATE `mem` SET `uM` = ( SELECT IF ((SELECT (`uM` - %d) FROM `mem` WHERE `uid` = %d) > 0), (SELECT (`uM` - %d) FROM `mem` WHERE `uid` = $d), 0 ) I don't think its set up right I'm trying a if else conditional: (pseudo) UPDATE memtable SET money = ( ( IF money - # > 0 ) SET money = money - # ELSE SET money = 0 ) WHERE uid = # get what I'm tryna do? Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 yes, i understand, but i think it's impossible to do it this way. and you need to have a separate statement in the if else statement. that's why there is that select Quote Link to comment Share on other sites More sharing options...
Demonic Posted February 26, 2008 Author Share Posted February 26, 2008 I think I'll solve my problem back at TLKPHP guys over there gave me 2 different codes: http://www.talkphp.com/mysql-databases/2342-complicated-mysql-if-statement-update.html#post11458 UPDATE cash, ( SELECT @fValue:=1.99, @fCash:=(cash - @fValue), @fCash:=IF(@fCash > 0, cash - @fValue, 0) FROM cash WHERE id = 1 ) AS fCashSET cash = @fCashWHERE id = 1 && http://www.talkphp.com/mysql-databases/2342-complicated-mysql-if-statement-update.html#post11459 UPDATE cash_table SET cash_value = IF (cash_value - %d > 0, cash_value - %d, 0) WHERE cash_id = %d; Learned something new I think. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 26, 2008 Share Posted February 26, 2008 before you become too deep into writing complicated queries ask your self if I can do this faster in php first, odds are you can do it much quicker in php and save your self the time of writing this complicated if/else strucutred queries. MySQL is great, but when you have the lobster of php sitting there why do you want to take the boring t-bone steak over it (t-bone steak = mysql). yes they all come out the same, but one goes in easier. To do it in php SELECT the value FROM record Calculate new value. UPDATE the record. To do it in MySQL UPDATE the record. I'll have the T-bone. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted February 26, 2008 Share Posted February 26, 2008 yes for a simple example like this it works, but sometimes the lobster makes so much more sense because you need to present the data and update it. Not to mention mysql versions have a lot more variance than php versions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.