22

I'm getting some weird results, so I need to just check myself...

SELECT *
FROM table
WHERE complete != 0
AND pending != 1

To be clear, these are allowed:

pending = 0, complete = 0
pending = 1, complete = 1
pending = 0, complete = 1

THis is NOT allowed to be returned from my query:

pending = 1, complete = 0

What am I missing here...?

7
  • Is it just that i need parenthesis around (complete != 0 AND pending != 1) ?
    – Shackrock
    Commented Nov 15, 2011 at 0:56
  • You should not need parentheses. That query should work, maybe you should post more detail about the actual query you are running (or the table). If that is the query, word for word, then that is very strange behavior. Commented Nov 15, 2011 at 0:57
  • I doubt parens would make a difference. Can you modify your question with the table structure? My first guess would be that either complete or pending weren't int data types
    – Kai Qing
    Commented Nov 15, 2011 at 0:58
  • @LoganSerman nope, that's it... the answer below was correct.
    – Shackrock
    Commented Nov 15, 2011 at 1:02
  • @KaiQing Answer below worked. TINYINT 1 fields.
    – Shackrock
    Commented Nov 15, 2011 at 1:03

2 Answers 2

41

Try:

SELECT *
FROM table
WHERE NOT (complete = 0
AND pending = 1)

or

SELECT *
FROM table
WHERE !(complete = 0
AND pending = 1)

EDIT: I went and looked at: http://dev.mysql.com/doc/refman/4.1/en/expressions.html

1
  • 1
    This. The original clause is too restrictive: It only allows complete = 1; pending = 0.
    – Dan J
    Commented Nov 15, 2011 at 1:00
8

You need to use OR, not AND. Your expression leaves out any combination where complete = 0 or pending = 1, which is too restrictive. Try the following:

SELECT *
FROM Table
WHERE complete != 0 OR pending != 1;
                    ^^ change AND to OR

Sample: http://sqlize.com/G8j6sFqo09

Not the answer you're looking for? Browse other questions tagged or ask your own question.