Reputation: 21
I want get full column from one table and select sum from another table that have same id.
Eg:
table1 table2
id target id target achived
1 40 1 20
2 50 2 25
3 66
4 80
and i want to select all from table 1 and fill achived target results on it.
Eg:
id target target achived
1 40 20
2 50 25
3 66 0
4 80 0
how can i do that using mysql
Upvotes: 0
Views: 297
Reputation: 1247
Below query results in 0
instead of NULL
SELECT t1.id, target, COALESCE(target_achieved, 0) AS target_achieved
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.id;
Upvotes: 0
Reputation: 11
Use this query:
select table1.*, case when table2.target_achieved is null
then 0
else table2.target_achieved
end as target_achieved
from table1 left join table2
on table1.id = table2.id
order by table1.id
Upvotes: 1