Jump to content


Photo

adding the values of two columns together


  • Please log in to reply
3 replies to this topic

#1 Justnew

Justnew
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 22 November 2003 - 09:57 PM

I am trying to add a value to an existing value in the coumn by using the select statement

select tea, mango, pepper price + profit as price from products

the profit have a default value of 200. when I run the query I only get the value of the profit
Can someone help me out

#2 Kriek

Kriek
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationFlorida

Posted 22 November 2003 - 10:44 PM

Method no.1

SELECT tea, mango, pepper, price + profit AS total FROM products

Method no.2

SELECT tea, mango, pepper, (price + profit) AS total FROM products

Jon Kriek · PHP Freaks
Sometimes the need to mess with their heads ...
... outweighs the millstone of humiliation.


#3 Justnew

Justnew
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 November 2003 - 01:03 PM

Thank you for your reply I have tried the code but the price column becomes blank no values is desplayed under the column name Price

#4 Kriek

Kriek
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationFlorida

Posted 23 November 2003 - 07:24 PM

Justnew, if you utilized either of my previous examples, the price column individually is non-existent. This is due to the fact that we have combined the values of the price and profit columns and then proceeded to alias that sum as total with the AS keyword. I do not recommend aliasing two columns with an original column name. To clarify why your previous method did not work is because the AS keyword is optional when aliasing a SELECT expression. So by not separating the pepper and price columns with a single coma you effectively aliased the pepper, price, and profit columns as price.
Jon Kriek · PHP Freaks
Sometimes the need to mess with their heads ...
... outweighs the millstone of humiliation.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users