简体   繁体   English

为什么我的查询没有返回正确的结果?

[英]Why is my query not returning proper results?

I am trying to exclude the records that have channel = all and category = Increase Customer Loyalty .我正在尝试排除具有channel = allcategory = 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.在我的 SQL 的某个地方,我做错了什么,因为执行它会排除所有行。 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.但是您的 where 会查看结果记录应满足的逻辑表达式。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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