我拿着號的四個不同領域的行的表:人,雜物,里程和包裝袋。由於目前還沒有數據,所有這些字段對於每一行都是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 != ''
你問了一個聲明的幫助,但是你有沒有報價在你的問題的聲明。 –
有點想避免張貼它,因爲它很大,但確定。 –
@matthias:你從來沒有聽說過的'WHERE contry的IN( '阿勒格尼', '布魯姆', '卡特羅格斯', '卡尤加', '...')'的語法? – knittl