【问题标题】:Access SQL - Multiple OR conditions using checkboxes访问 SQL - 使用复选框的多个 OR 条件
【发布时间】:2013-06-17 10:03:17
【问题描述】:

我的查询,哪种作品如下:

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];

此查询的所有四个部分中的WHERE 子句试图确定三个提到的复选框(识字、算术或贫困)是否被选中。可以检查这三者的任意组合以获得我想要的结果。

原则上,查询有效。但是,输出会为第三部分返回两个结果,为第四部分返回两个结果。

如果我只定义一个复选框来运行查询,那么:

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

查询工作正常,只是添加一个或多个这些条件似乎会导致问题。

我也尝试过使用 =-1 定义真值,这会返回相同的问题。

非常感谢。

【问题讨论】:

    标签: sql ms-access ms-access-2003


    【解决方案1】:

    看看这是否更清楚。您不需要 IIF 函数来检查 WHERE 子句中的 Yes/No 值。另外,需要括号来表达逻辑:(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];
    

    问候

    【讨论】:

    • 太棒了,谢谢你,谢谢你解释它。学习 SQL 的漫漫长路还在继续。
    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多