夏期劇場
夏期劇場

Reputation: 18325

MySQL Query for Multiple Columns to be FILTERED?

Lets say i have 1 Table only with multiple columns. Lets say:

Fruits
--------------------------------------
id  | name        | country
--------------------------------------
1   | Banana      | china
2   | Orange      | japan
3   | Apple       | japan
4   | Apple       | china
5   | Banana      | usa
6   | Orange      | china

Then the simple query to SELECT all Fruits where id < 6.
It is:

SELECT * FROM fruits WHERE id < 6

Then it will return:

--------------------------------------
id  | name        | country
--------------------------------------
1   | Banana      | china
2   | Orange      | japan
3   | Apple       | japan
4   | Apple       | china
5   | Banana      | usa

Then by appending above query, how can i go on to EXCLUDE something more.
For example:

Do NOT want to see, any "APPLE" from "CHINA".

So the final result must be:

--------------------------------------
id  | name        | country
--------------------------------------
1   | Banana      | china
2   | Orange      | japan
3   | Apple       | japan
5   | Banana      | usa

This is again without the row that having if, name='Apple' AND country='china' TOGETHER!.

I would say:

SELECT * FROM fruits WHERE id < 6 ... AND DON'T RETURN this condition where name='Apple' and country='china' TOGETHER

Upvotes: 0

Views: 103

Answers (2)

CloudyMarble
CloudyMarble

Reputation: 37566

Try:

SELECT * 
FROM fruits 
WHERE (id < 6)
AND NOT (name = 'Apple' AND country = 'china')

Or:

SELECT * 
FROM fruits 
WHERE (id < 6)
AND id NOT IN (SELECT id 
               FROM fruits
               WHERE name = 'Apple'
               AND country = 'china')

Upvotes: 2

echo_Me
echo_Me

Reputation: 37233

try this

 SELECT * FROM fruits WHERE 
                 (name ,country) not in ( select 'Apple' ,'china' from Fruits)

 HAVING id < 6

DEMO HERE

Upvotes: 1

Related Questions