IFNULL (COUNT ('id'), 0) in Codeigniter

I believe that an error occurred in this line in Codeigniter using active records, but I cannot understand the syntax in the second line using IFNULL () and COUNT ()

$this->db->select('places.*, category.*') ->select('IFNULL(COUNT("places_reviews.place_id"), 0) AS num_reviews') ->from('places') ->join('category', 'places.category_id = category.category_id') ->join('places_reviews', 'places_reviews.place_id = places.id', 'left') ->where('places.category_id', $category_id) ->group_by('places.id') ->limit($limit, $offset) ->order_by($sort_by, $sort_order); 
+6
source share
1 answer

Add false after the SELECT statement. CodeIgniter tries to avoid the inverse statement and does not know how to do it correctly. false will say that it is not.

 ->select('IFNULL(COUNT(`places_reviews.place_id`), 0) AS `num_reviews`', false) 

EDIT : In COUNT("places_reviews.place_id") quotation marks must be backward.

+16
source

Source: https://habr.com/ru/post/887648/


All Articles