I have 3 tables mr, mrd, and sm. Its structure is as below
table : Mr
id | location_id | is_processed
----------------------------
1 2 0
2 2 0
3 1 0
table mrd
id | mr_id | product_id | is_generated
---------------------------------------
1 1 6 0
2 1 8 0
3 2 6 0
table sm
product_id | qty
-----------
6 40
6 80
6 50
what I need is
product_id | qty
-----------------
6 170
8 0
This is what I tried
SELECT DISTINCT(mrd.product_id),sum(sm.qty) FROM mr
JOIN mrd ON mr.id = mrd.mr_id
join sm on mrd.product_id = sm.product_id
WHERE mr.to_location = 2 AND mr.is_processed = 0 AND mrd.is_generated = 0
GROUP BY mrd.product_id, sm.qty
converted the laravel to SQL for easy