what is this sql query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

what is this sql query?

Post by m2babaey »

Hi
I cannot figure out this query:

Code: Select all

SELECT symbol,SUM(IF(t_type='sell',-shares,shares)) AS shares,
                                                           MAX(price) AS maxprice, MIN(price) AS minprice
                                                           FROM transactions
                                                          WHERE completed=0
                                                          GROUP BY symbol
                                                          ORDER BY shares
My problem is "-shares"; what does it mean?
how about this: IF(t_type='sell',-shares,shares)
t_type and shares are table fields
Thanks for your help
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: what is this sql query?

Post by onion2k »

IF(t_type='sell',-shares,shares) is an if statement in SQL. It's saying "if t_type is 'sell' return the number in the column `shares` as a negative, else return it as a positive". In PHP it'd be..

Code: Select all

if ($t_type=="sell") {
  return -1*$shares;
} else {
  return $shares;
}
Post Reply