Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从表中选择产品总数和缺货产品计数_Mysql_Sql_Select_Count - Fatal编程技术网

Mysql 从表中选择产品总数和缺货产品计数

Mysql 从表中选择产品总数和缺货产品计数,mysql,sql,select,count,Mysql,Sql,Select,Count,我有三张表:产品,产品到商店,商店 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 要查找全部产品,我可

我有三张表:
产品
产品到商店
商店

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

要查找全部产品,我可以运行查询以获取product表中启用产品状态的所有产品

select count(*) from product where status=1

total             name
2              Store 1
2               store 2
要查找缺货产品总数,在加入所有3个表并使用
按门店分组\u id
后,我可以运行以下查询:

Select count(*) as outofproducts from product where quantity=0;
结果如下:

outofproducts     name
1                Store 1
1                store 2

但我希望在单个查询中组合上述两个结果,如下所示:

outofproducts  total  name
1              2      Store 1
1              2      store 2

您可以加入相关的子查询进行计数

  select t1.name, t1.outofproducts, t2.total
  from(
      select b.id, b.name , count(*) outofproducts 
      from product_to_store c  
      inner join product a on a.id = c.product_id
      inner  join store b on a.id = c.store_id 
      where a.quantity = 0
      group by  b.id, b.name 
  ) t1 
  inner join (
      select  b.id, b.name , count(*) total
      from product_to_store c 
      inner join product a on a.id = c.product_id
      inner  join store b on a.id = c.store_id 
      group by  b.id, b.name

  ) t2 on t1.id = t2.id

您可以从存储表开始查询,这样我们将获得作为存储表数据的总行数。
然后对每个商店使用嵌套查询以获取产品和产品总数

select 
(Select count(*) as outofproducts from product_to_store  ps inner join product p on p.id = ps.product_id where quantity=0 and ps.store_id = s.id  ) as outofproducts ,
(Select count(*) as count from product_to_store  ps inner join product p on p.id = ps.product_id where ps.store_id = s.id  ) as totalCount,
s.name
from store  s 

您将使用条件聚合IOPN,即对条件求和/计数:

select
  s.name,
  sum(p.quantity > 0) as in_stock,
  sum(p.quantity = 0) as out_of_stock,
  count(*) as total
from store s
join product_to_store ps on ps.store_id = s.id
join product p on p.id = ps.product_id
group by s.name
order by s.name;

这利用了MySQL的true=1,false=0。如果您不喜欢,请将
sum(p.quantity=0)
替换为
sum(p.quantity=0时为1,否则为0结束)
count(p.quantity=0时为1结束)

也添加一个适当的数据样本,而不仅仅是预期的结果……”但我希望在单个查询中组合上述两个结果,如下所示:哪两个结果我只看到一个..“”,若要查找总产品,我可以运行查询以获取产品状态为启用状态的product表中的所有产品。“什么查询?添加表架构..”。。这些桌子怎么样relatated@scaisEdge现在检查一下。我已经添加了。但是没有它也可以理解。此查询用于获取所有产品数据。我想在单个查询库的单个表中计算总产品和缺货产品的数量。您的解决方案也可以工作,但使用多个select查询并获取总产品数为0的结果。我想要@thorsten提到的结果。你的答案仍然正确。