说我有这样一张桌子:
表: @Transactional
void copyUser(HttpServletRequest request, HttpServletResponse response) {
User copyUser = getUserFromXML(idUser);// it get from Client
copyUser.setParentid(getLoginUser());
userDAO.save(copyUser);
}
}
businesses
我希望在搜索名称中的某些单词时获取+----+------------------------+
| id | name |
+----+------------------------+
| 1 | Carolyn Wong Cakes |
| 2 | Cupcakin Cackes Shop |
| 3 | Wong Cackes Shop |
| 4 | Indie Cakes & Pastries |
+----+------------------------+
。
例如,我想搜索:best matched result
这是3个单词
我在上面的搜索中使用此查询:
Wong Cackes Shop
我希望SELECT * FROM businesses WHERE (
name LIKE '% Wong %' OR
name LIKE '% Cackes %' OR
name LIKE '% Shop %'
)
成为第一个匹配的结果,但结果是:
record 3
//与Carolyn Wong Cakes
匹配的单词2
//与Cupcaking Cackes Shop
匹配的单词2
//与Wong Cackes Shop
匹配的单词如何在其他结果之上搜索单词并获得最匹配结果的记录?
像这样:
3
//与Wong Cackes Shop
匹配的单词3
Cupcaking Cackes Shop
编辑:我的Carolyn Wong Cakes
LIKE方法也存在问题。它不会得到以3个单词之一开头或结尾的结果。因为word-boundaries
中的spaces
个
例如:
LIKE '% word %'
//与之匹配
Wong[space]
匹配
[space]Wong[space]
//与
感谢。
答案 0 :(得分:2)
以下应该做的伎俩
SELECT
name,
(( name LIKE '%Wong%')+( name LIKE '%Cackes%') +(name LIKE '%Shop%')) as total
FROM businesses WHERE (
name LIKE '%Wong%' OR
name LIKE '%Cackes%' OR
name LIKE '%Shop%'
)
ORDER BY total DESC
您甚至可以在此处删除where子句,因为您只对点击总数感兴趣。
为了克服空间的问题,只需在like-clause中消除它们。 %wong%
将与[space]wong
或[space]wong[space]
或wong[space]
答案 1 :(得分:0)
它正确地执行,因为它只搜索单词。在您的情况下,您应该搜索整个字符串Wong Cackes Shop
(OR),最好是实施FULL TEXT
搜索