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 "); Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/ 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.... Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476518 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 Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476522 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 Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476529 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. Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476544 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 Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476576 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 Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476580 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. Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476625 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 Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476628 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 Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476639 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 Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476656 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. Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-476876 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? Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-477145 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 Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-477170 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. Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-477228 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. Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-477277 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. Link to comment https://forums.phpfreaks.com/topic/93012-can-you-do-math-with-sql-for-example/#findComment-477343 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.