1

我有以下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 本书等。我需要运行第二个查询来获取此信息,还是有办法在我的查询中执行此操作?
谢谢吉姆

4

5 回答 5

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'
于 2012-06-05T16:01:04.123 回答
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)

此函数将为您提供最后一个查询的行号。

于 2012-06-05T15:09:44.063 回答
0

听起来您需要使用 Group By 和 Count()

(当然,这将是一个不同的查询)

当您使用 Count() 和 Group by - 确保您没有包含在无法聚合的 Select 列中...

于 2012-06-05T15:06:33.707 回答
0

它可能使用 SQL 查询来完成,但您也可以使用 PHP 代码来完成。循环遍历结果并计算总数,如下所示:

foreach ($results as $row) {
    $location_count [$row['location_id']]++;
}
echo $location_count [986]; // outputs 17
于 2012-06-05T15:07:25.797 回答
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
于 2012-06-05T15:07:45.513 回答