改进我的搜索SQL查询

时间:2016-10-05 16:27:41

标签: php mysql

有人可以帮助改进我的SQL查询。这是一个网站搜索。以下是查询:

SELECT j_author.author_id as authorID, author_name, author_description, author_cat, author_city, author_state, points, (SELECT SUM(rate_value) FROM j_rating WHERE j_rating.author_id = authorID ) AS rating_value
    FROM j_author
    JOIN j_author_category ON j_author.author_cat = j_author_category.author_cat_id
    WHERE author_name LIKE '%" . $keyword . "%'
    OR author_city LIKE '%" . $keyword . "%'
    ORDER BY rating_value DESC

它有效,但它不完全是我想要的。例如,如果我搜索“edu”并且有一个名为educate的作者或城市,则会返回作者或城市。但如果搜索“受过教育的人”,则不返回任何结果。

我也尝试过使用:

SELECT j_author.author_id as authorID, author_name, author_description, author_cat, author_city, author_state, points, (SELECT SUM(rate_value) FROM j_rating WHERE j_rating.author_id = authorID ) AS rating_value,             
MATCH(j_author.author_name, j_author.author_city) AGAINST ('$keyword' IN BOOLEAN MODE) AS score
FROM j_author  
JOIN j_author_category ON j_author.author_cat = j_author_category.author_cat_id
WHERE MATCH(j_author.author_name, j_author.author_city) AGAINST ('$keyword' IN BOOLEAN MODE)
ORDER BY `score` DESC

但每次我使用这种方法时,当我搜索'edu'时,它都不会返回任何结果。

我想要的是如果“受过教育的人”是输入的搜索关键字,它应该使用“受过教育的”和“人”作为单独的关键词进行搜索,并返回与任何关键字匹配的结果。此外,当'edu'是关键字时,它应该返回所有在其名称或城市中使用edu的作者

1 个答案:

答案 0 :(得分:0)

如果您无法在将值传递到查询之前拆分文本,则可能是您正在寻找的内容:

Transact-SQL: How do I tokenize a string?

如果您对预先确定的字符分开查询并对每个"令牌执行查询,那么您应该能够实现目标。