MySQL匹配搜索中的比较运算符

时间:2015-02-23 13:38:20

标签: php mysql regex

下面是SQL语句,我很难放置一个where语句来获得匹配产生的分数,只返回得分超过6的产品。

SELECT product_id, image, title, part_num, price, discount_id, vat, 
       MATCH (title,part_num,description) 
       AGAINST ('SEARCH HERE') AS score 
FROM main_products AS p 
WHERE status = 1 
     AND MATCH(title,part_num,description) AGAINST ('SEARCH HERE') 
ORDER BY score desc, title ASC

我试过但这不起作用。它会导致此错误:文档#1054 - 'where子句'中的未知列'得分'

SELECT product_id, image, title, part_num, price, discount_id, vat, 
      MATCH (title,part_num,description) 
      AGAINST ('SEARCH HERE') AS score 
FROM main_products AS p WHERE status = 1 AND score > 6 
     AND MATCH(title,part_num,description) AGAINST ('SEARCH HERE') 
ORDER BY score desc, title ASC

2 个答案:

答案 0 :(得分:0)

这是怎么回事?

SELECT product_id, image, title, part_num, price, discount_id, vat, 
      MATCH (title,part_num,description) 
      AGAINST ('SEARCH HERE') AS score 
FROM main_products 
WHERE status = 1 
      AND MATCH(title,part_num,description) AGAINST ('SEARCH HERE')
HAVING score > 6 
ORDER BY score desc, title ASC

答案 1 :(得分:0)

MySQL首先使用WHERE条件来检索行,然后选择SELECT列。

因此当MySQL选择行时,“得分”并不为人所知。

你必须使用HAVING score > 6,因为Angga在帖子中说要使用它,或者WHERE MATCH(...) AGAINST(...) > 6正如Alex在评论中所说的那样。

两者都会返回相同的结果,但性能考虑应该让你更喜欢使用WHERE,因为它不会对列进行额外的工作以过滤选择(比如使用HAVING时),它只会读取索引文件以供选择