在 Codeigniter 中使用 $this->db->like() 返回错误/丢失的结果
我有一个现有的 SQL 查询,它可以完美地按照我想要的方式工作:
$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);
但是现在我想通过在上面添加一行来向查询添加一个 LIKE 子句以获得:
$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);
但是它给了我不准确的结果。例如,当我让搜索字符串 $term = "hong" 时,我有 3 行“name”列与“hong”匹配,即。 (香港咖啡厅,香港咖啡厅,拉面红),我只会返回(香港咖啡厅,香港咖啡厅)。现在,如果 $term =“hong kong”,我只会返回“香港咖啡馆”之一,而不是两者都返回。
还有一个更让我困惑!有一行称为“Dozo”。当$term = 'dozo'时,没有返回结果!
有什么想法为什么会发生这种情况吗?
实际生成的 SQL 抱歉,它出现在 1 行
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_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
更新
已解决。这是一个分页问题,将错误的变量传递给 LIMIT 子句。谢谢!
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_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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的实际查询来看,您的
offset
是从1
而不是0
开始,这样它将忽略第一条记录(位于偏移量0
)。所以对于这种情况:
显然什么都不会被返回。
From your actual query, your
offset
is beginning from1
instead of0
this way it'll ignore the first record (at offset0
).So for the case:
Nothing will be returned obviously.