我试图获得基于类别和关键字的搜索结果(使用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在数据库中确实没有任何条目或帖子,但我的查询使用其他类别返回结果......
答案 0 :(得分:0)
注意括号,几乎与(listings_categories.lc_id = 17 OR listings_categories.lc_parent = 17)
和ls_keywords LIKE ...
周围的IF语句一样。这会强制必须满足两个条件,而不是您拥有的多个条件:
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