2011-08-13 72 views
0

我拿着號的四個不同領域的行的表:人,雜物,里程和包裝袋。由於目前還沒有數據,所有這些字段對於每一行都是0。這些行還有兩個重要的字段,稱爲「縣」,其價值是紐約州的一個縣,「水下」只是1/0(是/否)字段。因此,他們是這個樣子......SQL語句返回只有兩行

County  Underwater People Debris Miles Bags 
Richmond    0  0  0  0 0 
Rockland    1  0  0  0 0 
Nassau    0  0  0  0 0 
Queens    1  0  0  0 0 
Niagara    0  0  0  0 0 

爲了將所有的這一起PHP頁面,我使用很長的SQL語句,有很多工會和縣基本上分離的結果紐約市,長島和紐約州北部,以及其他兩個關於水下和水下的問題,然後是總數。我認爲一次使用的長語句比六次單獨查詢對同一個表更有效,並且會爲我節省一些代碼行。我在所有數據爲0之前使用測試編號,並且它工作正常。現在所有的數據都是0,結果集是全0的一行,然後是一行NULL,沒有別的。由於我使用foreach PHP循環來解析數據,因此我的算法很難實現。我也注意到,如果我即使只將1行數據(即使只有1個字段)更改爲非零值,我也會在結果集中得到另一行。

任何人都可以解釋這種現象和解決辦法或許可供選擇?如果需要,我可以更深入。

這是順便說一下有問題的聲明。備用牆...

SELECT SUM(people) AS people, SUM(debris) AS debris, SUM(miles) AS miles, SUM(bags) AS bags 
FROM `table` 
WHERE county='The Bronx' OR county='Kings' OR county='New York' OR county='Queens' OR county='Richmond' 
UNION 
SELECT SUM(people) AS people, SUM(debris) AS debris, SUM(miles) AS miles, SUM(bags) AS bags 
FROM `table` 
WHERE county='Nassau' OR county='Suffolk' 
UNION 
SELECT SUM(people) AS people, SUM(debris) AS debris, SUM(miles) AS miles, SUM(bags) AS bags 
FROM `table` 
WHERE county='Albany' OR county='Allegany' OR county='Broome' OR county='Cattaraugus' OR county='Cayuga' OR county='Chautauqua' OR county='Chemung' OR county='Chenango' OR county='Clinton' OR county='Columbia' OR county='Cortland' OR county='Delaware' OR county='Dutchess' OR county='Erie' OR county='Essex' OR county='Franklin' OR county='Fulton' OR county='Genesee' OR county='Greene' OR county='Hamilton' OR county='Herkimer' OR county='Jefferson' OR county='Lewis' OR county='Livingston' OR county='Madison' OR county='Monroe' OR county='Montgomery' OR county='Niagara' OR county='Oneida' OR county='Onondaga' OR county='Ontario' OR county='Orange' OR county='Orleans' OR county='Oswego' OR county='Otsego' OR county='Putnam' OR county='Rensselaer' OR county='Rockland' OR county='Saratoga' OR county='Schenectady' OR county='Schoharie' OR county='Schuyler' OR county='Seneca' OR county='Steuben' OR county='StLawrence' OR county='Sullivan' OR county='Tioga' OR county='Tompkins' OR county='Ulster' OR county='Warren' OR county='Washington' OR county='Wayne' OR county='Westchester' OR county='Wyoming' OR county='Yates' 
UNION 
SELECT SUM(people) AS people, SUM(debris) AS debris, SUM(miles) AS miles, SUM(bags) AS bags 
FROM `table` 
WHERE underwater = '0' AND county != '' 
UNION 
SELECT SUM(people) AS people, SUM(debris) AS debris, SUM(miles) AS miles, SUM(bags) AS bags 
FROM `table` 
WHERE underwater = '1' AND county != '' 
UNION 
SELECT SUM(people) AS people, SUM(debris) AS debris, SUM(miles) AS miles, SUM(bags) AS bags 
FROM `table` 
WHERE county != '' AND fname != '' AND lname != '' 
+0

你問了一個聲明的幫助,但是你有沒有報價在你的問題的聲明。 –

+0

有點想避免張貼它,因爲它很大,但確定。 –

+0

@matthias:你從來沒有聽說過的'WHERE contry的IN( '阿勒格尼', '布魯姆', '卡特羅格斯', '卡尤加', '...')'的語法? – knittl

回答

4

使用UNION ALL而不是UNIONUNION運算符將在兩個結果集之間丟棄重複行,非常類似於單個結果集的SELECT DISTINCT。 (或者,選擇縣名太,因爲這將導致結果行含有不相同的數據。)


爲了消除從查詢NULL的行(其是在閒置數據集合函數像SUM()結果套)只是做這樣的事情:

SELECT * FROM (
    your query here 
) AS x 
WHERE x.people IS NOT NULL 

另外,在查詢中改變SUM(x)每次發生於COALESCE(SUM(x), 0)

+0

這是做到了。我現在唯一的問題是,6行中的一行返回所有NULL,因爲對於那個特定的SELECT語句,沒有結果(沒有行滿足'Underwater'= 1的條件)。無論如何,我可以有這個默認爲一行0而不是一行NULL?或者我將不得不在前端測試NULL並打印出0? –

+0

@Mathias:更新了我的答案,以包含該問題的解決方案。 – cdhowie

+0

我將COALESCE添加到了我所有的SUM(x)函數中。將所有的NULL更改爲0,這使得一個很好的故障安全。謝謝。 –