volume one
volume one

Reputation: 7553

SQL Where Clause To Match Against Both Conditions Simultaneously

I don't know how to phrase my question title for what I'm about to ask.

I have a SELECT query that must not return any rows if the combination of my where clause is true. Here is my example code:

SELECT 
 *
FROM
 MyTable m1
WHERE
 (m1.User != '1' AND m1.Status != '1')

But what I am trying to ask SQL is: "only return rows when the User is not '1' AND his status is not '1' at the same time. If this combination is not true, then its okay to return those rows".

So if the User is "1" and Status is "2" then that is fine to return those rows.

Seems simple but I can't visualize how to do it... help please?

Upvotes: 3

Views: 2637

Answers (3)

vasin1987
vasin1987

Reputation: 2012

SELECT 
 *
FROM
 MyTable m1
WHERE
 (m1.User <> '1' AND m1.Status <> '1')

There are many ways of doing this :)

Upvotes: -1

volume one
volume one

Reputation: 7553

Just answered my own question.... here is the answer. 'OR' doesn't test for combination of both being true.

Solution:

SELECT 
 *
FROM
 MyTable m1
WHERE NOT
 (m1.User = '1' AND m1.Status = '1')

Because both conditions have to be true for it not to return the rows. Both = AND, Either = OR.

Upvotes: 4

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

You may try this using OR instead of AND:

SELECT 
 *
FROM
 MyTable m1
WHERE
 !(m1.User = '1' OR m1.Status = '1')

Upvotes: 0

Related Questions