Jump to content


Photo

Finding the diference between two values


  • Please log in to reply
1 reply to this topic

#1 seran128

seran128
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 29 October 2006 - 04:11 AM

I have a table that looks like this

tbl_product

ID        Cost
1          5.23
2          3.76
7          25.22


now I want to run a query that returns the value of the difference between two product id's

like

psudeo code
finalcost = (select cost from tbl_products where id = '1') - (select cost from tbl_products where id = '2')

in this case finalcost should be 1.47

Is there a way to do this in one SQL statment

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 29 October 2006 - 09:03 AM

SELECT a.price - b.price as diff
FROM prices a JOIN prices b
ON a.price_id = 1 AND b.price_id = 2
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users