I have 2 tables.
table 1 (projects): id, name, type
table 2 (project_ratings): project_id, rating
Some projects do not have ratings.
SELECT `p`.`id`, `p`.`name`, AVG(pr.rating) FROM (`projects` p) JOIN `project_ratings` pr ON `p`.`id` = `pr`.`project_id` WHERE `p`.`type` = 'group' OR `p`.`type` = 'user';
I want to return all projects and return NULL if there are no ratings. This query returns only those who have ratings.
I tried left join, right join, full join, one and the same thing.
Using Active CodeIgniter Entries:
$this->db->select("p.id, p.name, AVG(pr.rating)"); $this->db->from('projects p'); $this->db->join('project_ratings pr', 'p.id = pr.project_id'); $this->db->where('p.type', 'group'); $this->db->or_where('p.type', 'user'); $res = $this->db->get();
What am I missing?
source share