MySQL UNION 查询正确处理 3 个或更多单词

发布于 2024-12-05 03:15:22 字数 203 浏览 1 评论 0原文

我必须请求你的帮助来解决这个问题。

我的网站有一个搜索字段,假设用户在“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烦人精 2024-12-12 03:15:22

也许你应该使用全文搜索

SELECT brand, name, MATCH (brand,name) AGAINST ('Korg X 50') AS score
FROM products WHERE MATCH (brand,name) AGAINST ('Korg X 50')

May be you should use full-text search

SELECT brand, name, MATCH (brand,name) AGAINST ('Korg X 50') AS score
FROM products WHERE MATCH (brand,name) AGAINST ('Korg X 50')
溇涏 2024-12-12 03:15:22

据我了解,您不需要 UNION 但需要类似

SELECT * FROM table1 
WHERE CONCAT(field1, field2) LIKE '%your_string%' 

在客户端,您可以删除 your_string 中出现的所有字符(如空格、连字符等)用户输入,且不能位于 field1field2 中。

因此,用户输入 Korg X 50 以及 Korg X-50 变为 KorgX50

As far as I understand you don't need UNION but something like

SELECT * FROM table1 
WHERE CONCAT(field1, field2) LIKE '%your_string%' 

On client side you get rid of all characters (like space, hyphen, etc) in your_string that appears in user input and cannot be in field1 or field2.

So, user input Korg X 50 as well as Korg X-50 becomes KorgX50.

白云不回头 2024-12-12 03:15:22

您将需要获取某种形式的可搜索文本。

要么解析出多个关键字的输入并分别匹配每个关键字,要么尝试将它们全部附加在一起并匹配以相同方式附加的列。

您还需要一个正则表达式,或者可能是一个更简单的搜索和替换,以在比较之前删除附加后的空格和破折号。

一般来说,允许用户搜索开放式文本字符串比“我使用什么联合”更复杂......理想情况下,您还会担心轻微的拼写错误、大写错误以及关键字顺序。

您可以考虑将所有关键字从正常记录中提取到与每个产品相关的单独关键字列表中,然后使用该列表来执行搜索。

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.

请别遗忘我 2024-12-12 03:15:22

如果您不想解析用户输入并按原样使用,那么您将需要使用这样的查询

select * from products where concat_ws(' ',brand,name) = user_input -- or
select * from products where concat_ws(' ',brand,name) like %user_input%

但是,如果用户输入名称“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

select * from products where concat_ws(' ',brand,name) = user_input -- or
select * from products where concat_ws(' ',brand,name) like %user_input%

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文