简体   繁体   中英

Why is my query not returning proper results?

I am trying to exclude the records that have channel = all and category = Increase Customer Loyalty . These 2 conditions combined because I have records with this category but different channel. Somewhere in my SQL I am doing something wrong because executing it it excludes all the rows. Eg all of the records when I expect to see half of them.

SELECT * FROM `flows_predefined`
WHERE platform = 'shopify'
AND status = 1
AND (
    category != 'Increase Customer Loyalty'
    AND channel != 'all'
)
ORDER BY `flows_predefined`.`id` DESC;

The problem is that you want to exclude the records which meet the criteria of

    category = 'Increase Customer Loyalty'
    AND channel = 'all'

but your where looks to the logical expression that the resulting records should meet. So you need to negate this condition:

NOT (
    category = 'Increase Customer Loyalty'
    AND channel = 'all'
}

and now let's apply this to your query:

SELECT * FROM `flows_predefined`
WHERE platform = 'shopify'
AND status = 1
AND NOT (
    category = 'Increase Customer Loyalty'
    AND channel = 'all'
)
ORDER BY `flows_predefined`.`id` DESC;

or, if you prefer the != operand:

SELECT * FROM `flows_predefined`
WHERE platform = 'shopify'
AND status = 1
AND (
    category = 'Increase Customer Loyalty'
    OR channel = 'all'
)
ORDER BY `flows_predefined`.`id` DESC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM