a.a表结构
item_code
quantity_purchased
b.b表结构
stock_id
qty
我想把a表的数量总和求出来以外也把b表的总和求出来,a表的item_code字段对应b表stock_id字段,两个表没在一个数据库里面我想要的结果:
item_code-----SUM(`quantity_purchased`)-------SUM(`qty`)
0001-00-------1000--------1500
0002-00-------2000--------1000
0003-00-------3000--------2000请问语句怎么写
item_code
quantity_purchased
b.b表结构
stock_id
qty
我想把a表的数量总和求出来以外也把b表的总和求出来,a表的item_code字段对应b表stock_id字段,两个表没在一个数据库里面我想要的结果:
item_code-----SUM(`quantity_purchased`)-------SUM(`qty`)
0001-00-------1000--------1500
0002-00-------2000--------1000
0003-00-------3000--------2000请问语句怎么写
这里使用了inner join 是完全匹配两个表 如果需要某个表的所有记录一定出现在结果集里 把这个表放在左边并且换成left join
from (
select item_code,sum(quantity_purchased) as total1
from a.a
group by item_code
)t1,
(
select stock_id,sum(qty) as total2
from b.b
group by stock_id
)t2
where t1.item_code=t2.stock_id
(select sum(`qty`) from b where stock_id=a.item_code)
from a
group by item_code