选择求和条件联接表mysql

时间:2019-01-02 05:29:50

标签: java mysql

我想将“ tbpembelian”中的“ sisastock”列与“ tbproduk”加起来。但我的结果只有一行。

我的查询:

const mongoose = require('mongoose');
const User = mongoose.model('users');

app.put('/api/place/save', (req, res)=>{
  User.findByIdAndUpdate(req.body.userId, {
    "$addToSet" : { savedPlace: req.body.placeId}
  }, (err, user)=>{
    err ? res.send(err) : res.send(user)
  })
})

表“ tbproduk”

enter image description here

表“ tbpembelian”

enter image description here

我想要的结果

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试这可能会有所帮助,因为您已在SQL SELECT语句中列出了未封装在SQL SUM函数中的一列,所以必须使用SQL GROUP BY子句。

select pe.idprd,  p.nmprd,  pe.tglmasuk, k.idkat, d.iddvs, sum(pe.sisastock) as
'totalsisastock'
from tbproduk as p 
left join tbpembelian as pe on pe.idprd = p.idprd 
join tbkategori as k on p.idkat = k.idkat 
join tbdivisi as d on p.iddvs = d.iddvs 
group by pe.idprd,  p.nmprd,  pe.tglmasuk, k.idkat, d.iddvs
having sum(pe.sisastock) > 0 
order by pe.tglmasuk DESC