Mudali Derick
Mudali Derick

Reputation: 31

How to select all recods from 1 table, and a computed value from another table, and display all results

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

Answers (2)

Miya
Miya

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

Ravi
Ravi

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

Related Questions