List:General Discussion« Previous MessageNext Message »
From:Mark Goodge Date:August 21 2010 8:22am
Subject:Re: Performing subtraction between fields
View as plain text  
On 21/08/2010 07:25, b@stripped wrote:
> Hello,
>
> For simplicity's sake, let's say I have three fields, A, B and C, all
> of which are integers. I'd like the value of C to be equal to A less B
> (A-B). Is there a way I can perform this calculation? I'm guessing it
> would happen when I INSERT a row and specify the values for A and B.
> Feel free to direct me to the fine manual I should have read.

Given two variables, $a and $b:

INSERT INTO mytable SET A = $a, B = $b, C = $a - $b

or

INSERT INTO mytable (A, B, C) VALUES ($a, $b, $a - $b)

or, if you've previously inserted A and B:

UPDATE mytable SET C = A - B

http://dev.mysql.com/doc/refman/5.1/en/numeric-functions.html

Simples :-)

Mark
Thread
Performing subtraction between fieldsb21 Aug
  • Re: Performing subtraction between fieldsMark Goodge21 Aug
  • Re: Performing subtraction between fieldsAshish Mukherjee21 Aug
    • Re: Performing subtraction between fieldsAshley Stars21 Aug
      • Re: Performing subtraction between fieldsAshish Mukherjee21 Aug
        • Re: Performing subtraction between fieldsDan Nelson22 Aug
  • Re: Performing subtraction between fieldsChris W21 Aug