我有以下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本书等。 我需要运行第二个查询来获取此信息,还是有办法在我的查询中执行此操作? 谢谢 吉姆
答案 0 :(得分: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'
答案 1 :(得分:0)
听起来你需要使用Group By和Count()
(这将是一个不同的课程查询)
当您使用Count()和Group by时 - 请确保您不包含在无法聚合的Select列中...
答案 2 :(得分:0)
可以使用SQL查询来完成,但您也可以使用PHP代码来完成。循环结果并计算总数,如下所示:
foreach ($results as $row) {
$location_count [$row['location_id']]++;
}
echo $location_count [986]; // outputs 17
答案 3 :(得分: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
答案 4 :(得分: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)
此函数将为您提供上次查询的行号。