0

Ok I am having some trouble with this one, for some reason this is not working.

I am trying to get a list of trades made by a user or to the user that are still active.

SELECT * FROM trades WHERE active = 1 and trader = 1 or tradee = 1 ORDER BY trade_date ASC

This returns non active trades because the tradee or trader equals 1. I have even tried other variations.

SELECT * FROM trades WHERE trader = 1 OR tradee = 1 AND active = 1 ORDER BY trade_date ASC

Any help would be appreciated.

Mike
  • 11
  • 2
  • 1
    The assiduous use of parentheses may help. – Strawberry Mar 12 '14 at 16:19
  • Understanding operator precedence is really one of the most elementary programming skills. See http://stackoverflow.com/questions/1241142/sql-logic-operator-precedence-and-and-or – Bill Karwin Mar 12 '14 at 16:21

3 Answers3

0

If I understand correctly, you want parenthesis:

SELECT * 
FROM trades 
WHERE 
    (trader = 1 OR tradee = 1)
    AND active = 1 
ORDER BY trade_date ASC
planestepper
  • 3,277
  • 26
  • 38
0

Use parentheses (and read up on boolean algebra):

SELECT * FROM trades WHERE active = 1 and (trader = 1 or tradee = 1) ORDER BY trade_date ASC
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

Place your two OR conditions inside of parentheses:

SELECT
         *
FROM
         trades
WHERE
         active = 1
AND
         (
                  trader = 1
         OR       tradee = 1
         )
ORDER BY
         trade_date ASC
David H. Bennett
  • 1,822
  • 13
  • 16