开发者

Multiple joins in Codeigniter (Active Records)

开发者 https://www.devze.com 2023-03-04 20:42 出处:网络
I\'m trying to retrieve results from my database with details spanning across tables \'places\', \'category\' and \'reviews\', then sort by number of reviews. When I add the 2nd join, only one row is

I'm trying to retrieve results from my database with details spanning across tables 'places', 'category' and 'reviews', then sort by number of reviews. When I add the 2nd join, only one row is retrieved.

$this->db->select('places.*, category.*')
            ->from('places')
            ->join('category', 'places.category_id = category.category_id')
            ->join('places_reviews', 'places_reviews.place_id = places.id')
            ->where('places.category_id', $category_id)
            ->limit($limit, $offset)
            ->order_by($sort_by, $sort_order);

I did not add in the IFNULL(COUNT(places_reviews('review_id')) AS 'num_reviews', 0) into the select() function to开发者_StackOverflow中文版 make the code cleaner.

Any ideas?


I think what you want is a LEFT JOIN, try:

->join('places_reviews', 'places_reviews.place_id = places.id', 'left')
0

精彩评论

暂无评论...
验证码 换一张
取 消