私は4つの異なるフィールド(人、残骸、マイル、バッグ)を持つ行のテーブルを持っています。これらのフィールドは、まだデータがないため、すべての行で0です。これらの行には、ニューヨーク州の郡の1つである「郡」と、1/0(はい/いいえ)フィールドの「水中」という2つの重要なフィールドもあります。そこで彼らは、PHPのページのために一緒にグループ化するためには、このすべてをこのような何か...UNIONで6つのSELECTを持つSQL文は、2行しか返しません。
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
を見て、私は組合のたくさんの長いSQL文を使用して、基本的には郡によって結果を分離していますNew York City、Long Island、Upstate New Yorkからのもの、そして水中ではなく水中での2つの他のクエリ、次に総計。私は、一度使用された長いステートメントが、同じテーブルに対する6つの別々のクエリよりも効率的であると考えました。そして、それはコードのいくつかの行を節約します。私はすべてのデータが0になる前にテスト番号を使用していましたが、うまくいきました。すべてのデータが0になったので、結果セットはすべて0の行、次にNULLの行、それ以外は何もありません。私はforeach PHPループを使用してデータを解析しているので、それは私のアルゴリズムとうまくいっています。私はまた、たとえ1行だけのデータであっても(1フィールドであっても)0以外の値に変更すると、結果セットに別の行が追加されることに気付きました。
誰でもこの動作と解決方法を説明することはできますか、代替手段を提供することはできますか?私は、必要に応じてさらに詳細に行くことができます。
これは、問題のステートメントです。壁の待機...
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:あなたは 'contry IN( 'Allegany'、 'Broome'、 'Cattaraugus'、 'Cayuga'、 '...')の構文について聞いたことがありませんか? – knittl