Jump to content

Archived

This topic is now archived and is closed to further replies.

Demonic

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

Recommended Posts

Wondering if you can do math with SQL:

 

mysql_query("UPDATE `member` SET `cash` += 1 WHERE `id` = NUM ");

Share this post


Link to post
Share on other sites

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....

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Uh its the same thing..wth..

 

Anyways anyone know how to use the mysql if statements?  I can't quite understand it.

Share this post


Link to post
Share on other sites

yes but it reproduces the query which I wanted to look at because the mysql error messages are cut off usually

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×
×
  • 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.