Using $this->db->like() in Codeigniter returns wrong/missing results
I have an existing SQL query that works perfectly as I want it:
$this->db->select('places.*, category.*')
->select('COUNT(places_reviews.place_id) AS num_reviews')
->select('(places_popularity.rating_1 + 2*places_popularity.rating_2 + 3*places_popularity.rating_3 + 4*places_popularity.rating_4 + 5*places_popularity.rating_5)/(places_popularity.rating_1 + places_popularity.rating_2 + places_popularity.rating_3 + places_popularity.rating_4 + places_popularity.rating_5) AS average_rating')
->from('places')
->join('category', 'places.category_id = category.category_id')
->join('places_reviews', 'places_reviews.place_id = places.id', 'left')
->join('places_popularity', 'places_popularity.place_id = places.id', 'left')
->where('places.category_id', $category_id)
->group_by('places.id')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
However now I want to add a LIKE clause to the query by adding one more line to the above to get:
$this->db->select('places.*, category.*')
->select('COUNT(places_reviews.place_id) AS num_reviews')
->select('(places_popularity.rating_1 + 2*places_popularity.rating_2 + 3*places_popularity.rating_3 + 4*places_popularity.rating_4 + 5*places_popularity.rating_5)/(places_popularity.rating_1 + places_popularity.rating_2 + places_popularity.rating_3 + places_popularity.rating_4 + places_popularity.rating_5) AS average_rating')
->from('places')
->join('category', 'places.category_id = category.category_id')
->join('places_reviews', 'places_reviews.place_id = places.id', 'left')
->join('places_popularity', 'places_popularity.place_id = places.id', 'left')
->where('places.category_id', $category_id)
->like('places.name', $term)
->group_by('places.id')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
However it is giving me inaccurate results. For example, when i let the string being searched $term = "hong" and I have 3 rows where the 'name' column matches "hong" ie. (Hong Kong Cafe, Hong Kong Cafe, Ramen Hong), I will only get (Hong Kong Cafe, Hong Kong Cafe) returned. Now if $term = "hong kong", I only get one of the 'Hong Kong Cafe' returned and not both.
Another one puzzles me even further! There is a row called 'Dozo'. When $term = 'dozo', no result is returned!
Any ideas why this is happening?
Actual SQL generated Sorry it appears in 1 line
SELECT `places`.*, `category`.*, COUNT(places_reviews.place_id) AS num_reviews, (places_popularity.rating_1 + 2*places_popularity.rating_2 + 3*places_popularity.rating_3 + 4*places_popularity.rating_4 + 5*places_popularity.rating_5)/(places_popularity.rating_1 + places_popularity.rating_2 + places_popularity.rating_3 + places_popularity.rating_4 + places开发者_C百科_popularity.rating_5) AS average_rating FROM (`places`) JOIN `category` ON `places`.`category_id` = `category`.`category_id` LEFT JOIN `places_reviews` ON `places_reviews`.`place_id` = `places`.`id` LEFT JOIN `places_popularity` ON `places_popularity`.`place_id` = `places`.`id` WHERE `places`.`category_id` = 1 AND `places`.`name` LIKE '%Dozo%' GROUP BY `places`.`id` ORDER BY `average_rating` desc LIMIT 1, 3
UPDATE
SOLVED. Its a pagination problem that passes the wrong variable to the LIMIT clause. Thanks!
From your actual query, your offset
is beginning from 1
instead of 0
this way it'll ignore the first record (at offset 0
).
So for the case:
Another one puzzles me even further! There is a row called 'Dozo'. When $term = 'dozo', no result is returned!
Nothing will be returned obviously.
精彩评论