Jump to content

Can you do math with SQL? For example....


Demonic

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

		$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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.