Reputation: 31
How do I select all records from one table, plus a computed value from another table (Like average), and display all results including those from the first table with out corresponding value from second table?
I have tried this
SELECT
table1.*,
AVG(table2.column) AS average
FROM table1
JOIN
table2 ON table1.id = table2.id;
But it only displays only results with average.
What should I do please?
Upvotes: 3
Views: 360
Reputation: 30
Please use below query to fetch the details.
SELECT table1.*,
(SELECT AVG(table2.column) FROM table2 WHERE table1.pkid = table2.fkid) as Average
FROM table1;
Upvotes: 1
Reputation: 1172
This will solve ur problem....
SELECT *,
(select AVG(table2.column) FROM table2 where table1.id = table2.id) AS average
from table1
Upvotes: 1