【发布时间】:2018-08-01 10:37:06
【问题描述】:
我要 3 张桌子:product、product_to_store、store
product table
id quantity status
1 1 1
2 0 1
3 0 1
4 23 1
product_to_store table
store_id product_id
1 1
2 2
1 3
2 4
store table
id name
1 store1
2 store2
要查找总产品,我可以运行查询以获取表产品中启用产品状态的所有产品。
select count(*) from product where status=1
total name
2 Store 1
2 store 2
要查找总缺货产品,我可以在加入所有 3 个表并使用 group by store_id 后运行以下查询:
Select count(*) as outofproducts from product where quantity=0;
结果是这样的:
outofproducts name
1 Store 1
1 store 2
但我希望将以上 2 个结果组合成单个查询,如下所示:
outofproducts total name
1 2 Store 1
1 2 store 2
【问题讨论】:
-
也添加适当的数据样本,而不仅仅是预期的结果..
-
"但我希望将以上 2 个结果组合成单个查询,如下所示:" 什么两个结果我只看到一个..
-
""要查找全部产品,我可以运行查询以获取表 product 中产品状态已启用的所有产品。" 什么查询?
-
添加您的表格架构 .. 表格是如何关联的
-
@scaisEdge 现在检查。我已经添加了。但是没有它也可以理解。