4 개의 다른 필드 (사람, 잔해, 마일 및 가방)가있는 행의 테이블이 있습니다. 아직 모든 데이터가 없기 때문에이 모든 필드는 모든 행에 대해 0입니다. 이 행에는 뉴욕주의 카운티 중 하나 인 '카운티'라는 2 개의 중요한 필드와 1/0 (예/아니오) 필드 인 '수중'필드가 있습니다. 그래서 같은 식으로 ...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
위해서는 그룹이 모든 것을 함께 PHP 페이지에 대해, 나는 노동 조합의 많은 긴 SQL 문을 사용하여 본질적으로 군에 의해 결과를 분리하고있어 뉴욕시, 롱 아일랜드 출신, 뉴욕 주립대, 그리고 수중과 수중을 제외한 다른 두 가지 쿼리를 통해 총계를 계산합니다. 필자는 한 번 사용 된 긴 명령문이 동일한 테이블에 대한 여섯 개의 개별 쿼리보다 더 효율적이라는 것을 알았고 코드의 일부 라인을 절약 할 수있었습니다. 모든 데이터가 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