如何选择所有车间的所有产品的数量,包括子车间。
Product_ID Brand Shop_ID
1 Bosco 10
2 Kenzo 11
3 SARA 11
4 iStyle 13
5 Patric 13
6 Kenzo 13
7 SARA 13
8 iStyle 14
9 Patric 15
Shop_ID Shop Parent_Shop_ID
10 Shop1
11 Shop2 10
12 Shop3 10
13 Shop4 11
14 Shop5 11
15 Shop6 14
结果必须是:
Shop_ID Shop Quantity_Brands
10 Shop1 9
11 Shop2 8
12 Shop3 0
13 Shop4 4
14 Shop5 2
15 Shop6 1
什么是正确的选择?
发布于 2018-06-04 14:33:48
根据给定的数据和所有商店中的产品请求,您的问题的唯一可能解决方案是
select count(*) from products
但我猜你的问题更难吧?!
发布于 2018-06-04 14:55:53
我已经更改了数据库结构和其他两个检查,这适合你这个方法,你可以快速获得信息。
假设QTC =某些产品的可用数量我有另一个建议,您也可以将可用数量存储在单独的表中,这样您就不必在主表中更新详细信息,因为频繁更新主表不是一个好的做法表产品
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| p_id | int(50) | NO | PRI | NULL | auto_increment |
| brand | varchar(50) | NO | | NULL | |
| shp_id | int(50) | NO | | NULL | |
| qtc | int(25) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
餐桌商店
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| shp_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(11) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
SELECT shop.name, SUM(product.qtc) FROM shop INNER JOIN product ON shop.shp_id = product.shp_id GROUP BY shop.shp_id
https://stackoverflow.com/questions/50674305
复制相似问题