获取Mysql的数量

获取Mysql的数量,mysql,Mysql,查询1(第一个表-入库) select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s left join reagent r on r.id = s.reagent_id group by r.name select sum(t.consumption)as 'stock out total' ,r.name from stock_out s inner join test_consumption t on

查询1(第一个表-入库)

select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s 
left join reagent r on r.id = s.reagent_id group by r.name
select sum(t.consumption)as 'stock out total' ,r.name from stock_out s 
inner join test_consumption t on s.consumption_id = t.id 
inner join reagent r on r.id = t.reagent_id group by r.name 
查询2(第二个表-缺货)

select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s 
left join reagent r on r.id = s.reagent_id group by r.name
select sum(t.consumption)as 'stock out total' ,r.name from stock_out s 
inner join test_consumption t on s.consumption_id = t.id 
inner join reagent r on r.id = t.reagent_id group by r.name 
问题1

select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s 
left join reagent r on r.id = s.reagent_id group by r.name
select sum(t.consumption)as 'stock out total' ,r.name from stock_out s 
inner join test_consumption t on s.consumption_id = t.id 
inner join reagent r on r.id = t.reagent_id group by r.name 
stock in | r.name
100 |里根2
100 |里根3
问题2

select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s 
left join reagent r on r.id = s.reagent_id group by r.name
select sum(t.consumption)as 'stock out total' ,r.name from stock_out s 
inner join test_consumption t on s.consumption_id = t.id 
inner join reagent r on r.id = t.reagent_id group by r.name 
缺货| r.name
40 |里根2
20 |里根3
我尝试过这样做,但它不会减法,因为由于group by,每个嵌套的select语句都返回“不止一列”错误消息。 我也试着把这组股票剔除,但最后把两种不同的股票合并,然后减去

select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s 
left join reagent r on r.id = s.reagent_id group by r.name
select sum(t.consumption)as 'stock out total' ,r.name from stock_out s 
inner join test_consumption t on s.consumption_id = t.id 
inner join reagent r on r.id = t.reagent_id group by r.name 
SELECT QUERY1 – QUERY2 as ‘current stocks’
预期结果

select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s 
left join reagent r on r.id = s.reagent_id group by r.name
select sum(t.consumption)as 'stock out total' ,r.name from stock_out s 
inner join test_consumption t on s.consumption_id = t.id 
inner join reagent r on r.id = t.reagent_id group by r.name 
流通股| r.name
60 |里根2
80 |里根3

为什么不将这两个查询转储到内联视图中,然后将它们连接在一起

select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s 
left join reagent r on r.id = s.reagent_id group by r.name
select sum(t.consumption)as 'stock out total' ,r.name from stock_out s 
inner join test_consumption t on s.consumption_id = t.id 
inner join reagent r on r.id = t.reagent_id group by r.name 
像这样的事情会让你走的。别名为“i”的内联视图为入库,别名为“o”的内联视图为出库:

select sum(s.liquid_quantity) as 'stock in total' from stockin_detail s 
left join reagent r on r.id = s.reagent_id group by r.name
select sum(t.consumption)as 'stock out total' ,r.name from stock_out s 
inner join test_consumption t on s.consumption_id = t.id 
inner join reagent r on r.id = t.reagent_id group by r.name 
select i.name,i.in-ifnull(o.out,0) as 'current stock'
from
 (
  select sum(s.liquid_quantity) as in,r.name 
  from stockin_detail s 
  left join reagent r on r.id = s.reagent_id 
  group by r.name
 ) i
 left outer join
 (
  select sum(t.consumption)as 'out' ,r.name from stock_out s 
  inner join test_consumption t on s.consumption_id = t.id 
  inner join reagent r on r.id = t.reagent_id 
  group by r.name
 ) o on i.name = o.name;

那么你的问题是什么?预期结果是否输入错误?Reagent2加20,Reagent3加60对我来说更有意义……哦,对不起。抄错了桌子。谢谢。。编辑:感谢它的工作。我将别名“in”改为“sin”,因为in是mysql中的内置函数。它成功了。谢谢