簡體   English   中英

在 UNION 中有 6 個 SELECT 的 SQL 語句僅返回 2 行

[英]SQL statement with 6 SELECTs in UNION returns only 2 rows

我有一張包含四個不同數字字段的行表:人、碎片、英里和包。 現在所有這些字段對於每一行都是 0,因為還沒有數據。 這些行還有兩個重要字段,稱為“County”,其值是紐約 state 的縣之一,以及“Underwater”,它只是一個 1/0(是/否)字段。 所以他們看起來像這樣......

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 語句,其中包含許多 UNION,並且基本上將結果按紐約市的縣、長島縣和紐約州北部的縣分開,以及然后是水下和非水下的另外兩個查詢,然后是總計。 我認為使用一次的長語句比對同一個表的六個單獨查詢更有效,並且它可以為我節省一些代碼行。 我在所有數據都為 0 之前使用了測試編號,並且效果很好。 現在所有數據都是 0,結果集是一行全 0,然后是一行 NULL,僅此而已。 由於我正在使用 foreach PHP 循環解析數據,因此我的算法有點搞砸了。 我還注意到,如果我僅將 1 行的數據(即使它只有 1 個字段)更改為非零,那么我將在結果集中獲得另一行。

誰能解釋這種行為和解決方案,或者提供替代方案? 如果需要,我可以更深入地了解 go。

順便說一句,這是有問題的陳述。 掛牆待機...

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 != ''

使用UNION ALL而不是UNION UNION運算符將丟棄兩個結果集之間的重復行,就像SELECT DISTINCT對單個結果集所做的那樣。 (或者 select 也是縣名,因為這會導致結果行包含不同的數據。)


要從查詢中消除 NULL 行(這是對空數據集的SUM()等聚合函數的結果),只需執行以下操作:

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

或者,將查詢中每次出現的SUM(x)更改為COALESCE(SUM(x), 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM