Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

with this query :


SELECT a.nid, a.stock, b.qty
FROM uc_product_stock a
INNER JOIN uc_order_products b ON a.nid = b.nid

I have the following result:


nid stock   qty
11  256     4
11  256     1
11  256     4
11  256     1
11  256     1
10  746     1
10  746     2
10  746     1

now i want to sum qty column with same id 11 and 10 i.e my result should be


 nid    stock   qty
 11     256     11
 10     746     4
share|improve this question
add comment

closed as off topic by dezso, ypercube, FreshPhilOfSO, Mark Storey-Smith, Leigh Riffel Feb 4 '13 at 21:38

Questions on Database Administrators Stack Exchange are expected to relate to database administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

You just need to group by the relevant columns, and use an aggregate functin (SUM in this case):

SELECT 
a.nid, a.stock, sum(b.qty)
FROM 
uc_product_stock a INNER JOIN uc_order_products b ON a.nid = b.nid
group by 
a.nid, a.stock

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.