MYSQL / Math question...

He has: 1,758 posts

Joined: Jul 2002

Hey guys, please excuse my ignorance, I'm not the best mathematician on the planet and I'm a bit confused.

Say I have two fields in my products database:

oldprice
saleprice

I know I can get the saving by doing

SELECT `oldprice` -  `saleprice` AS saving FROM  `products`'

What I need though is the saving as a percentage (i.e. if something was reduced from £50 to £25 you'd be saving 50% wouldn't you?)

Any ideas anyone?

Andy

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

use [="Courier New"]SELECT (1 - `saleprice` / `oldprice` ) AS savings FROM `products`[/] to get a true numeric value (.50 for 50%, .75 for 75%)

or us [="Courier New"]SELECT ((1 - `saleprice` / `oldprice` ) * 100 ) AS savings FROM `products`[/] to get just a whole number to display (75 for 75%)

-Greg

He has: 1,758 posts

Joined: Jul 2002

Brilliant! thanks for the help dude!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.