2012-06-05 3 views
1

에 대한 레코드의 총 수를 찾는 I 다음 MySQL의 쿼리가 : 완벽하게 작동하지만, 지금은 각 위치 ID에 대한 책의 총 수를 표시 할 필요가주어진 위치

SELECT sku, quantity, inventory.isbn13, author, title, pub_date, binding, 
     defect.defect, source, location from inventory 
     LEFT JOIN location ON inventory.location_id = location.location_id 
     LEFT JOIN source ON inventory.source_id = source.source_id 
     LEFT JOIN book ON inventory.isbn13 = book.isbn13 
     LEFT JOIN defect ON inventory.defect_id = defect.defect_id 
     LEFT JOIN book_condition ON book_condition.condition_id = defect.condition_id 
     WHERE quantity > '0' and location.location_id >= '986' and location.location_id <= '989' 
     ORDER BY inventory.location_id, sku 

합니다. 예를 들어 location_id 986에는 17 권의 책이 있고 location_id에는 34 권의 책이 있습니다. 이 정보를 얻기 위해 두 번째 쿼리를 실행해야합니까, 아니면 내가 가진 쿼리에서이 작업을 수행 할 수 있습니까?
감사 짐

+0

당신은 인벤토리의 또는보다 일반적인 상황에서 맥락에서이 정보를 원하십니까 "이 위치에 얼마나 많은 책이있다"? – Sebas

+0

선택한 각 위치 (이 경우에는 986 - 989)에 대한 도서의 총 수를 표시해야합니다. 감사합니다 – Jim

+0

다음 내 대답을 확인하십시오 – Sebas

답변

1

당신은 갈 :

SELECT 
    sku, 
    quantity, 
    inventory.isbn13, 
    author, 
    title, 
    pub_date, 
    binding, 
    defect.defect, 
    source, 
    location, 
    t.cnt 
FROM 
    inventory i 
     INNER JOIN 
      (
      SELECT 
       inventory.location_id, 
       COUNT(book.isbn13) as `cnt` 
      FROM inventory 
        LEFT JOIN book ON inventory.isbn13 = book.isbn13 
      GROUP BY inventory.location_id 
      ) t ON t.location_id = i.location_id 
     INNER JOIN location l  ON i.location_id = l.location_id 
     LEFT JOIN source   ON i.source_id = source.source_id 
     LEFT JOIN defect   ON i.defect_id = defect.defect_id 
     LEFT JOIN book_condition ON book_condition.condition_id = defect.condition_id 
WHERE 
    i.quantity > '0' 
AND l.location_id >= '986' 
AND l.location_id <= '989' 
+0

예제를 테스트하는 데 지연이 생겨서 미안하지만, 실행할 때 *에서 'sql'구문 오류가 발생합니다. 나는 그 책이. * 무슨 뜻인지 잘 모르겠다. 조금만 설명하면 오류를 바로 잡아서 코드를 다시 시험해 볼 수 있을까? 감사합니다 – Jim

+0

책은 책 테이블입니다. 왜 구문 오류가 있는지 궁금 해서요. stl checking ... – Sebas

+0

book.title에 몇 가지 변경 사항을 적용하여 책. *을 변경했습니다. 작동하는 것으로 보입니다. 나는 여전히 수학을 검증 할 필요가 있지만, 언뜻보기에 그것은 가까이서 보입니다. 도와 주셔서 감사합니다. – Jim

0

당신이 그룹으로 사용하고

이 (가의-물론 다른 쿼리 것)) (카운트해야 할 것 소리

당신은 백작()와 그룹에 의해를 사용하는 경우 - 집계 할 수없는 열 선택 ...

0

아마도 SQL 쿼리를 사용하여 수행 할 수 있지만 PHP 코드를 사용하여 수행 할 수도 있습니다. 결과를 통해 루프와 합계를 계산, 이런 식으로 뭔가 :

foreach ($results as $row) { 
    $location_count [$row['location_id']]++; 
} 
echo $location_count [986]; // outputs 17 
0
SELECT count(*), sku, quantity, inventory.isbn13, author, title, pub_date, binding, 
     defect.defect, source, location from inventory 
     LEFT JOIN location ON inventory.location_id = location.location_id 
     LEFT JOIN source ON inventory.source_id = source.source_id 
     LEFT JOIN book ON inventory.isbn13 = book.isbn13 
     LEFT JOIN defect ON inventory.defect_id = defect.defect_id 
     LEFT JOIN book_condition ON book_condition.condition_id = defect.condition_id 
     WHERE quantity > '0' and location.location_id >= '986' and location.location_id <= '989' 
     ORDER BY inventory.location_id, sku 
     GROUP BY location.location_id 
+0

게시물 주셔서 감사하지만 이것은 전체 레코드 수를 가진 마지막 위치 만 반환합니다. 쿼리 결과. – Jim

0

당신은 MySQL의 기능 'FOUND_ROWS'를 사용할 수에 영향을 미치거나 쿼리를 변경하지 않고, 내 지역의 예를보십시오.

mysql> select * from actor where actor_id>190; 
+----------+------------+-------------+---------------------+ 
| actor_id | first_name | last_name | last_update   | 
+----------+------------+-------------+---------------------+ 
|  191 | GREGORY | GOODING  | 2012-05-22 15:12:26 | 
|  192 | JOHN  | SUVARI  | 2012-05-22 15:12:26 | 
|  193 | BURT  | TEMPLE  | 2012-05-22 15:12:26 | 
|  194 | MERYL  | ALLEN  | 2012-05-22 15:12:26 | 
|  195 | JAYNE  | SILVERSTONE | 2012-05-22 15:12:26 | 
|  196 | BELA  | WALKEN  | 2012-05-22 15:12:26 | 
|  197 | REESE  | WEST  | 2012-05-22 15:12:26 | 
|  198 | MARY  | KEITEL  | 2012-05-22 15:12:26 | 
|  199 | JULIA  | FAWCETT  | 2012-05-22 15:12:26 | 
|  200 | THORA  | TEMPLE  | 2012-05-22 15:12:26 | 
|  205 | 1   | 2   | 0000-00-00 00:00:00 | 
|  206 | a   | b   | 0000-00-00 00:00:00 | 
+----------+------------+-------------+---------------------+ 
12 rows in set (0.00 sec) 

mysql> select FOUND_ROWS(); 
+--------------+ 
| FOUND_ROWS() | 
+--------------+ 
|   12 | 
+--------------+ 
1 row in set (0.00 sec) 

이 함수는 마지막 쿼리의 행 번호를 제공합니다. 여기

관련 문제