简体   繁体   English

访问SQL - 使用复选框的多个OR条件

[英]Access SQL - Multiple OR conditions using checkboxes

My query, which kind of works is as follows: 我的查询,哪种作品如下:

SELECT [copyright status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE (IIF(literacy,1,0)) OR (IIF(numeracy,1,0)) OR (IIF(poverty,1,0))
GROUP BY [copyright status]

UNION

SELECT null,
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE (IIF(literacy,1,0)) OR (IIF(numeracy,1,0)) OR (IIF(poverty,1,0))

UNION

SELECT [lw status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE (IIF(literacy,1,0)) OR (IIF(numeracy,1,0)) OR (IIF(poverty,1,0)) AND [lw status] = 'In Reserve'
GROUP BY [lw status]

UNION

SELECT [lw status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE (IIF(literacy,1,0)) OR (IIF(numeracy,1,0)) OR (IIF(poverty,1,0)) AND [lw status] = 'Published'
GROUP BY [lw status];

The WHERE clause in all the four parts of this query is trying to determine the three mentioned checkboxes (literacy, numeracy or poverty) are checked. 此查询的所有四个部分中的WHERE子句试图确定检查的三个复选框(识字,计算或贫困)。 Any combination of the three can be checked for the results that I would like. 可以检查三者的任何组合以获得我想要的结果。

In principle, the query works. 原则上,查询有效。 However, the output returns two results for the third part and two results for the fourth part. 但是,输出返回第三部分的两个结果和第四部分的两个结果。

If I run the query with just one checkbox defined, so: 如果我只用一个复选框定义运行查询,那么:

WHERE (IIF(literacy,1,0)) [lw status] = 'In Reserve'

The query works fine, its just adding in one or more of these conditions seems to cause the problems. 查询工作正常,它只是添加一个或多个这些条件似乎导致问题。

I've also tried defining true values by using =-1 which returns the same problems. 我也尝试使用= -1来定义真值,它返回相同的问题。

Many thanks. 非常感谢。

See if this is more clear. 看看这是否更清楚。 You don't need the IIF function to check a Yes/No value at the WHERE clauses. 您不需要IIF函数来检查WHERE子句中的是/否值。 Aditionally, parenthesis are required to express the logic: (x OR y OR z) AND w 通常,括号需要表达逻辑: (x OR y OR z) AND w

SELECT null as Status,
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE (literacy OR numeracy OR poverty)

UNION

SELECT [copyright status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE (literacy OR numeracy OR poverty)
GROUP BY [copyright status]

UNION

SELECT [lw status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE (literacy OR numeracy OR poverty) AND [lw status] = 'In Reserve'
GROUP BY [lw status]

UNION

SELECT [lw status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE (literacy OR numeracy OR poverty) AND [lw status] = 'Published'
GROUP BY [lw status];

Regards 问候

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

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