简体   繁体   中英

Select sum condition join table mysql

I want to sum column "sisastock" in "tbpembelian" join with "tbproduk". but my result just one row.

My query :

select 
    pe.idprd,  p.nmprd, sum(pe.sisastock) as 'totalsisastock', 
    pe.tglmasuk, k.idkat, d.iddvs 
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 
having 
    sum(pe.sisastock) > 0 
order by 
    pe.tglmasuk DESC

Table "tbproduk"

在此处输入图片说明

Table "tbpembelian"

在此处输入图片说明

The result I want

在此处输入图片说明

Try This it may help, Because you have listed one column in your SQL SELECT statement that is not encapsulated in the SQL SUM function, you must use the SQL GROUP BY clause.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM