gpt4 book ai didi

mysql - 获取Mysql数量

转载 作者:行者123 更新时间:2023-11-29 23:07:48 25 4
gpt4 key购买 nike

查询 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

查询 2(第二个表 - 缺货)

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

stock in  |r.name     
100 |Reagent2
100 |Reagent3

查询 2

stock out  |r.name     
40 |Reagent2
20 |Reagent3

我尝试这样做,但它不会减去,因为每个嵌套的 select 语句由于分组依据而返回“多于一列”错误消息。我还尝试删除该组,但最终梳理了两只不同的股票,然后减去了。

SELECT QUERY1 – QUERY2 as ‘current stocks’

预期结果

current stock|r.name  
60 |Reagent2
80 |Reagent3

最佳答案

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

这样的事情应该能让你继续下去。内联 View 别名“i”是库存,内联 View 别名“o”是库存:

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;

关于mysql - 获取Mysql数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28218612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com