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冊の本があります。この情報を取得するには、2番目のクエリを実行する必要がありますか、それとも私が持っているクエリでこれを行う方法はありますか?
ありがとうジム

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

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 に答える
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 に答える