![](/img/trans.png)
[英]Laravel limit result of multiple record table select based on if all records meet specific criteria
[英]Excluding records based on multiple criteria with specific values in two columns
信息:
我有一张包含三列的表:Book、Store_1 和 Store_2。 我正在使用 MS Access,但也熟悉 MySQL。
_______________________________________________
Book | Store_1 | Store_2 |
Lord of the Rings | Paper | |
Ender's Game | Digital | Digital |
Mistborn | Digital | |
Spines | | Paper |
Grapes of Wrath | | Digital |
Artemis Fowl | | |
Redwall | Paper | Paper |
Perloo the Bold | Digital | Paper |
_______________________________________________
问题
我需要编写一个查询,如果它满足以下任一条件,它将排除一行:
运行查询后,该表应如下所示:
_______________________________________________
Book | Store_1 | Store_2 |
Spines | | Paper |
Grapes of Wrath | | Digital |
Redwall | Paper | Paper |
Perloo the Bold | Digital | Paper |
_______________________________________________
我试过的
老实说,我不知道如何将所有三个标准组合成一个陈述。 我得到的最接近的......与我想要的相反,这就是:
SELECT *
FROM Books
WHERE (Store_2 IS NULL) OR (Store_1 AND Store_2 = "Digital") OR (Store_1 AND Store_2 IS NULL)
;
它给了我我不想要的书,这是一个开始,但我不知道如何扭转它。 在 WHERE 之后添加 NOT IN 时出现语法错误。 感谢您在正确道路上提供的任何帮助、建议或指导。
您的第二个NULL
条件是多余的,因为只有在第一个为真时它才能为真。 因此,您可以将表达式简化为:
SELECT *
FROM Books
WHERE Store_2 IS NOT NULL AND NOT (Store_2 <=> 'Digital' AND Store_1 <=> 'Digital')
输出:
Book Store_1 Store_2
Spines Paper
Grapes of Wrath Digital
Redwall Paper Paper
Perloo the Bold Digital Paper
这样做:
SELECT *
FROM Books
WHERE (Store_2 IS NOT NULL) AND (Store_1 <> 'Digital' OR Store_2 <> 'Digital') AND (Store_1 ÌS NOT NULL OR Store_2 IS NOT NULL)
;
@zip 很接近,但在第二个Store_1 IS NOT NULL
有一个隐藏的反引号,对于不等于“数字”或 NULL 的任何商店的空安全比较是检查Store_1
是否为空,因为 Store_2 不能为空,因为的第一个标准。
现在,如果满足这些条件之一,则会显示数据。
SELECT *
FROM Books
WHERE Store_2 IS NOT NULL
AND (
Store_1 <> 'Digital'
OR Store_2 <> 'Digital'
OR Store_1 IS NOT NULL
);
Null 的常见错误可能会启发 OP 在Ms Access
获得空安全。
最后一句话。
带有数字的字段名(特别是按顺序),闻起来像糟糕的规范化。 通常,您会有一个商店编号列,这使您可以拥有无限数量的商店。
为了稍后在列中显示带有商店编号的数据,您可以创建一个交叉表查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.