Mcbaloo Mcbaloo - 19 days ago 5
SQL Question

Using Sql to get result from two tables

TABLE1

-id-----name----partid
1 DDU 2
2 FDK 4
3 DDU 3


TABLE2

-partid----Amount-----
2 600
3 400
4 500


After the query, This is how i want my table to look like

name amount
DDU 1000


Note: The 1000 for DDU IS GOTTEN BY ADDING 600 AND 400. How do i achieve this using my sql query. Partid is a foreign key in Table1 and Primary Key of table2

Answer
SELECT t.name,SUM(s.amount)
FROM Table1 t
JOIN Table2 s
 ON(t.partid = s.partid)
GROUP BY t.name

You also should get FDK 500 in your result set , so I assume you need a WHERE clause ?

WHERE t.name = 'DDU'