簡體   English   中英

查找給定位置的記錄總數

[英]finding the total number of records for a given location

我有以下mysql查詢:

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

效果很好,但是現在我需要顯示每個位置ID的書籍總數。 例如location_id 986有17本書,location_id 987有34本書,等等。我是否需要運行第二個查詢來獲取此信息,或者是否可以在我擁有的查詢中執行此操作?
謝謝吉姆

干得好:

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'

聽起來您需要使用Group By和Count()

(這將是一個不同的課程查詢)

當您使用Count()和Group by時-確保您不包括無法聚合的Select列...

可以使用SQL查詢來完成,但是您也可以使用PHP代碼來完成。 循環瀏覽結果並計算總數,如下所示:

foreach ($results as $row) {
    $location_count [$row['location_id']]++;
}
echo $location_count [986]; // outputs 17
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

您可以使用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)

此函數將為您提供最后一個查詢的行號。

暫無
暫無

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

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