Skip to content
Advertisement

How to Inverse Where Clause in mysql Query?

I’m querying my MySQL “consumer_details” table where the age is > 30 and purchase_count > 100, by the same time I need to exclude the people who age is equal ( = ) 50 and purchase_count < 100

I’m running the following query right now :

Base Query :

SELECT * 
    FROM `consumer_details` 
    WHERE `age` > 30 AND `purchase_count ` > 100 
    ORDER BY `gender` DESC

Extended Query :

SELECT * 
    FROM `consumer_details` 
    WHERE `age` > 30 AND `purchase_count ` > 100  
         NOT IN (`age` = 50 AND `purchase_count ` < 100 ) 
    ORDER BY `gender` DESC

Advertisement

Answer

SELECT * 
FROM consumer_details 
WHERE   (age > 30 AND purchase_count > 100)
AND NOT (age = 50 AND purchase_count < 100) 
ORDER BY gender DESC
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement