MySQL:选择最接近的匹配? [英] MySQL: select the closest match?
问题描述
我想显示与产品最接近的相关项目.所以说我正在展示一个产品,样式编号是 SG-sfs35s.有没有办法选择最接近的产品款式编号?
I want to show the closest related item for a product. So say I am showing a product and the style number is SG-sfs35s. Is there a way to select whatever product's style number is closest to that?
谢谢.
回答您的问题.好吧,我绝对想保留前 2 个字母,因为这是制造商代码,但至于第一个破折号之后的部分,只要匹配最接近的即可.因此,例如 SG-sfs35s 将比 SG-sht64s 更匹配 SG-shs35s.我希望每当我执行 LIKE product_style_number 时,这都是有意义的,它只会拉出完全匹配.
to answer your questions. Well I definitely want to keep the first 2 letters as that is the manufacturer code but as for the part after the first dash, just whatever matches closest. so for example SG-sfs35s would match SG-shs35s much more than SG-sht64s. I hope this makes sense whenever I do LIKE product_style_number it only pulls the exact match.
推荐答案
通常没有一种简单的方法可以匹配大致相似的产品代码.
There normally isn't a simple way to match product codes that are roughly similar.
更加 SQL 友好的解决方案是创建一个新表,将每个产品映射到与其相似的所有产品.
A more SQL friendly solution is to create a new table that maps each product to all the products it is similar to.
该表要么需要手动维护,要么可以定期执行更复杂的脚本来更新它.
This table would either need to be maintained manually, or a more sophisticated script can be executed periodically to update it.
如果您的产品代码遵循一致的模式(类似产品的所有字母都相同,只是数字有所变化),那么您应该能够使用正则表达式来匹配类似产品项目.这个这里有文档...
If your product codes follow a consistent pattern (all the letters are the same for similar products, with only the numbers changing), then you should be able to use a regular expression to match the similar items. There are docs on this here...
这篇关于MySQL:选择最接近的匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!