在计算我的系统中传入的材料和材料的使用之后,如果有人想要进行调整,则会有一个调整项目。多个SUM和多个计算值
QUERY来料 - 使用的材料
select (select sum(jumlah) from warehouse where tgl_invoice >= '2013-03-17' AND tgl_invoice <='2013-03;18' and kode=a.kode) - COALESCE((select sum(jumlah) from use_material where tanggal >= '2013-03-17' AND tanggal <='2013-03;18' and kode_barang=a.kode),0) total, a.kode, a.nama from warehouse a group by a.kode;
+-------+---------+------------+ | total | kode | nama | +-------+---------+------------+ | 4 | ACLG001 | AC LG 1 pk | | 160 | P001 | Spindle | | 30 | S012 | Cable | +-------+---------+------------+
mysql> select * from adjusment;
结果:
+----+-------------+-------------+--------+--------+------------+---------------+ | id | kode_barang | nama_barang | status | jumlah | tanggal | user | +----+-------------+-------------+--------+--------+------------+---------------+ | 7 | P001 | Spindle | + | 10 | 2013-03-30 | Administrator | | 8 | P001 | Spindle | - | 5 | 2013-03-30 | Administrator | | 9 | S012 | Cable | + | 0 | 2013-03-30 | Administrator | +----+-------------+-------------+--------+--------+------------+---------------+
我算
select(select sum(jumlah) from adjusment where status='+') - (select sum(jumlah) from adjusment where status='-') as total,kode_barang,nama_barang from adjusment group by kode_barang;
+-------+-------------+-------------+ | total | kode_barang | nama_barang | +-------+-------------+-------------+ | 5 | P001 | Spindle | | 5 | S012 | Cable | +-------+-------------+-------------+
而且我去年股票这样的查询:
select (select sum(jumlah) from warehouse where tgl_invoice >= '2013-03-17' AND tgl_invoice <='2013-03;18' and kode=a.kode) - (select sum(jumlah) from use_material where tanggal >= '2013-03-17' AND tanggal <='2013-03:18' and kode_barang=a.kode) + COALESCE((select sum(jumlah) from adjusment where status='+' and kode_barang=a.kode),0) - COALESCE((select sum(jumlah) from adjusment where status='-' and kode_barang=a.kode),0) as total,a.kode,a.nama from warehouse a group by a.kode;
+-------+---------+------------+ | total | kode | nama | +-------+---------+------------+ | NULL | ACLG001 | AC LG 1 pk | | 165 | P001 | Spindle | | 30 | S012 | Cable | +-------+---------+------------+
结果应该是电缆= 35 和AC LG 1 PK = 4
什么错?
我想,2013-03; 18只是一个错字?也许你必须使用BETWEEN来比较日期?你能提供一个数据库计划吗? (用于涉及表) – bestprogrammerintheworld
我有编辑日期:) 数据库模式到哪个表? – yusronnube
你的问题是什么?这与http://stackoverflow.com/questions/15715323/ask-mysql-query-sum-different-table有何不同? – Barmar