vb.net - MYSQL Combing Two tables From Multiple Row -


i have 2 tables, 1 products, other new purchases products.

product table

item    qty 301       2 302       5 303       3 304       4 305       6 

purchases table

item     qty    status    date 302       5     used      09-15-2015 303       5     reserve   09-20-2015 301       5     used      09-20-2015 302       5     reserve   09-20-2015 304       5     used      10-15-2015 303       5     reserve   10-15-2015 

i want display how many quantity in product table, , join quantity of purchases table product table

this initial sql query

select product_name, product_quantity, quantity  products      left outer join purchases on products.id = purchases.product_id  product_status !='deleted' , status != 'used'`.  

however, returned the items purchases table reserve.

what want achieve is

item    qty    reserved qty    total 301       2     0                 2 302       5     5                 10 303       3     10                13 304       4     0                 4 305       6     0                 6 

update new sql query

select product_name, product_quantity, p.quantity  products      left outer join (select * purchases          status = 'reserved'          group product_id)  p on p.product_id = products.id  products.product_status !='deleted'` 

but returns 0 on reserved qty.

you there in terms of query except need sum quantity of reserved items , use group by, sort of this

select     pd.item 'item', pd.qty 'qty', ifnull(pr.reserved_quantity, 0) 'reserved qty', (pd.qty+ifnull(pr.reserved_quantity, 0)) 'total'     product pd     left outer join (select item, sum(qty) 'reserved_quantity' purchases `status`='reserve' group 1) pr on pd.item=pr.item 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -