在 Codeigniter 中使用 $this->db->like() 返回错误/丢失的结果

发布于 2024-11-06 09:35:17 字数 3240 浏览 0 评论 0原文

我有一个现有的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌无敌 2024-11-13 09:35:17

从您的实际查询来看,您的 offset 是从 1 而不是 0 开始,这样它将忽略第一条记录(位于偏移量 0)。

所以对于这种情况:

还有一个让我更加困惑!
有一行称为“Dozo”。什么时候
$term = 'dozo',没有返回结果!

显然什么都不会被返回。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文