MySQL UNION 查询正确处理 3 个或更多单词
我必须请求你的帮助来解决这个问题。
我的网站有一个搜索字段,假设用户在“Korg X 50”中写入,
在我的数据库表“产品”中,我有一个包含“X50”的归档“名称”和一个包含“Korg”的字段“品牌”。有没有办法使用 UNION 选项来获取正确的记录?
如果用户输入“Korg X-50”?
非常感谢 !
马泰奥
I've to ask your help to solve this problem.
My website has a search field, let's say user writes in "Korg X 50"
In my database in table "products" i have a filed "name" that holds "X50" and a field "brand" that hold "Korg". Is there a way to use the UNION option to get the correct record ?
And if the user enters "Korg X-50" ?
Thank you very much !
Matteo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许你应该使用全文搜索
May be you should use full-text search
据我了解,您不需要
UNION
但需要类似在客户端,您可以删除
your_string
中出现的所有字符(如空格、连字符等)用户输入,且不能位于field1
或field2
中。因此,用户输入
Korg X 50
以及Korg X-50
变为KorgX50
。As far as I understand you don't need
UNION
but something likeOn client side you get rid of all characters (like space, hyphen, etc) in
your_string
that appears in user input and cannot be infield1
orfield2
.So, user input
Korg X 50
as well asKorg X-50
becomesKorgX50
.您将需要获取某种形式的可搜索文本。
要么解析出多个关键字的输入并分别匹配每个关键字,要么尝试将它们全部附加在一起并匹配以相同方式附加的列。
您还需要一个正则表达式,或者可能是一个更简单的搜索和替换,以在比较之前删除附加后的空格和破折号。
一般来说,允许用户搜索开放式文本字符串比“我使用什么联合”更复杂......理想情况下,您还会担心轻微的拼写错误、大写错误以及关键字顺序。
您可以考虑将所有关键字从正常记录中提取到与每个产品相关的单独关键字列表中,然后使用该列表来执行搜索。
you will need to get some form of searchable text.
either parse out the input for multiple key words and match each separately, or perhaps try to append them all together and match to the columns appended in the same way.
you will also need either a regex, or maybe a simpler search and replace to get rid of spaces and dashes after the append before the comparison.
in general, allowing users to search for open ended text strings is more complicated than 'what union do i use'... you will ideally also be worried about slight misspellings and capitalization, and keyword order.
you may consider pulling all keywords out from your normal record into a separate keyword list associated with each product, then use that list to perform your searches.
如果您不想解析用户输入并按原样使用,那么您将需要使用这样的查询
但是,如果用户输入名称“Korg X-50”并且您的表包含“Korg”,则此查询将不会返回结果”和“X50”,那么您需要做一些其他事情来实现这一目标。您可以查看 http://dev.mysql.com /doc/refman/5.0/en/string-functions.html#function_soundex 但这不是一个完整的解决方案。寻找该示例的文本索引库:lucene
If you do not want to parse user input and use as it is, then you will need to use a query like this
However, this query won't return result if user enters name "Korg X-50" and your table contains "Korg" and "X50", then you need to do some other thing to achive this. You may look at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex however it won't be a complete solution. Look for text indexing libraries for that ex: lucene