我们有一个MySQL数据库,它有两个表,Warehouse和Crate
Warehouse:
WarehouseID (primary key)
Location (varchar)
Crate:
CrateID (primary key)
Warehouse (foreign key to a Warehouse record)
Max_Capacity (int) (the amount of boxes that can be packed into a crate)
我想编写一个查询,返回每个仓库的所有包装箱的max_capacity的总和。我遇到了重复条目和内连接的问题,所以我希望的是:
LOCATION: WAREHOUSE_ID: TOTAL_CAPACITY:
Shoreline 1 60
Bellevue 2 120
Ballard 3 200
每个仓库可能有10个左右的箱子。
答案 0 :(得分:1)
select w.location, w.warehouse_id, sum(c.capacity) as total_capacity
from warehouse w, crate c
where w.warehouse_id = c.warehouse_id
group by c.warehouse_id
答案 1 :(得分:0)
你应该查看mysql手册中的这些词:
这些可以帮助您组装所需的查询。
答案 2 :(得分:0)
只是仓库分组:
(假设crate表中的外键名为warehouseID)
SELECT
warehouse.location,
warehouse.warehouseID,
SUM(crate.max_capacity) AS total_capacity
FROM warehouse
INNER JOIN crate
ON crate.warehouseID = warehouse.warehouseID
GROUP BY
warehouse.warehouseID