please help me to make a query using mysql to show the products(product.name) that have total quantity SUM(goods.quantity) > 100 in all 5 warehouses, specifying the warehouse(warehouse.name) with the greatest quantity of each product. Here is the scheme of all tables for better understanding:
I am trying this query:
SELECT product.name AS product, warehouse.name AS warehouse
FROM product, warehouse, goods
WHERE goods.productId = product.id AND goods.warehouseId = warehouse.id
GROUP BY product.name, warehouse.name
HAVING SUM(goods.quantity)>100;
it gives me the following:
| product | warehouse |
|---|---|
| engine | New York |
| right back door | New York |
| main frame | London |
| left back door | London |
| back lights | London |
| steering wheel | Sydney |
| wheel | Toronto |
| car cleaner | Toronto |
But it only shows products that have goods.quantity > 100, not the SUM(goods.quantity) > 100 in all warehouses.
Besides, it doesn't show warehouse with the greatest quantity of each product, but any warehouse that has this product.