0

我正在尝试基于类别和关键字(使用 like 或 regexp)获取搜索结果,但问题是,我的查询还返回基于关键字的其他类别结果。

SELECT * FROM (`listings`)
LEFT JOIN `listing_categories` ON `listings`.`ls_id` = `listing_categories`.`ls_id`
LEFT JOIN `listings_categories` ON `listing_categories`.`lc_id` = `listings_categories`.`lc_id`
LEFT JOIN `users` ON `users`.`id` = `listings`.`ls_user_id`
WHERE `listings`.`ls_status` =  1
AND listings_categories.lc_id = 17
OR listings_categories.lc_parent = 17
AND listings.ls_keywords LIKE '%Test Broken Functionality%'
OR listings.ls_keywords LIKE '%Test%'
OR listings.ls_keywords LIKE '%Broken%'
OR listings.ls_keywords LIKE '%Functionality%'
GROUP BY `listings`.`ls_id`
ORDER BY MATCH(listings.ls_keywords) AGAINST('Test Broken Functionality') DESC

类别 ID 17 在数据库中实际上没有任何条目或帖子,但我的查询返回使用来自其他类别的结果...

4

1 回答 1

0

(listings_categories.lc_id = 17 OR listings_categories.lc_parent = 17)注意括号,几乎就像and周围的 IF 语句一样ls_keywords LIKE ...。这迫使必须满足两个条件,而不是您拥有的多个条件:

SELECT * FROM (`listings`)
LEFT JOIN `listing_categories` 
    ON `listings`.`ls_id` = `listing_categories`.`ls_id`
LEFT JOIN `listings_categories` 
    ON `listing_categories`.`lc_id` = `listings_categories`.`lc_id`
LEFT JOIN `users` 
    ON `users`.`id` = `listings`.`ls_user_id`
WHERE `listings`.`ls_status` =  1
    AND (listings_categories.lc_id = 17 OR listings_categories.lc_parent = 17)
    AND (listings.ls_keywords LIKE '%Test Broken Functionality%'
        OR listings.ls_keywords LIKE '%Test%'
        OR listings.ls_keywords LIKE '%Broken%'
        OR listings.ls_keywords LIKE '%Functionality%')
GROUP BY `listings`.`ls_id`
ORDER BY MATCH(listings.ls_keywords) AGAINST('Test Broken Functionality') DESC
于 2012-11-19T21:36:59.270 回答