2013-06-20 46 views
0

我的查詢,這樣的作品是如下條件:訪問SQL - 多或使用複選框

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定義真值,它返回相同的問題。

非常感謝。

回答

2

看看這是否更清楚。您不需要IIF函數來檢查WHERE子句中的是/否值。的方法,另外,括號中需要表達的邏輯:(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]; 

問候

+0

好極了,謝謝你,謝謝您的解釋它。學習SQL的漫長之路還在繼續。 – Squadinho