简体   繁体   English

MySQL-通过关联进行订单查询,无需全文本方法

[英]Mysql - Order query by relevance without full-text method

I want to know how can I achieve this. 我想知道如何实现这一目标。 I need to order a query by relevance but I cannot do that, I do not want to use the full-text + boolean mode, etc because I have a lot of records that do not appears this way (50%+ of regs) and so on, so I've create a query in a loop to search word by word and I exclude the irrelevant words by myself before doing the query, the result of the process for two words query is something like this: 我需要按相关性排序查询,但是我不能这样做,我不想使用全文+布尔模式,等等,因为我有很多记录不是这样(50%+ regs)出现,并且依此类推,所以我在一个循环中创建了一个查询,以逐字搜索,在执行查询之前,我自己排除了不相关的字,两个字查询的过程结果如下:

SELECT UsrNames, UsrLastNames 
FROM user
WHERE 1 AND (UsrNames LIKE '%FirstWord%'
            OR UsrLastNames LIKE '%FirstWord%'
            OR UsrCI LIKE '%FirstWord%'
            OR UsrEmail LIKE '%FirstWord%'
            OR UsrRUC LIKE '%FirstWord%'
            OR UsrCod LIKE '%FirstWord%'
            OR UsrPhone LIKE '%FirstWord%'
            OR UsrNames LIKE '%SecondWord%'
            OR UsrLastNames LIKE '%SecondWord%'
            OR UsrCI LIKE '%SecondWord%'
            OR UsrEmail LIKE '%SecondWord%'
            OR UsrRUC LIKE '%SecondWord%'
            OR UsrCod LIKE '%SecondWord%'
            OR UsrPhone LIKE '%SecondWord%') ORDER BY  UsrNames ASC 
 LIMIT 20
 OFFSET 0

It is right now in development, so the don't you care about the "WHERE 1" at the beginning. 现在正在开发中,因此一开始就不必关心“ WHERE 1”。

Hope this great comunity could help. 希望这个伟大的社区能有所帮助。

This will order by the number of matches: 这将根据匹配数进行排序:

ORDER BY IF(UsrNames LIKE '%FirstWord%', 1, 0)
        + IF(UsrLastNames LIKE '%FirstWord%', 1, 0)
        + IF(UsrCI LIKE '%FirstWord%', 1, 0)
        + IF(UsrEmail LIKE '%FirstWord%', 1, 0)
        + IF(UsrRUC LIKE '%FirstWord%', 1, 0)
        + IF(UsrCod LIKE '%FirstWord%', 1, 0)
        + IF(UsrPhone LIKE '%FirstWord%', 1, 0)
        + IF(UsrNames LIKE '%SecondWord%', 1, 0)
        + IF(UsrLastNames LIKE '%SecondWord%', 1, 0)
        + IF(UsrCI LIKE '%SecondWord%', 1, 0)
        + IF(UsrEmail LIKE '%SecondWord%', 1, 0)
        + IF(UsrRUC LIKE '%SecondWord%', 1, 0)
        + IF(UsrCod LIKE '%SecondWord%', 1, 0)
        + IF(UsrPhone LIKE '%SecondWord%', 1, 0) DESC,
    UsrNames ASC

FIDDLE 小提琴

The IF() expressions convert each of the comparisons to a number and totals them. IF()表达式将每个比较转换为一个数字并将它们相加。 If you wanted, you could even make some fields more important in the ranking by giving them a higher value than 1 . 如果需要的话,甚至可以通过赋予它们大于1值来使某些字段在排名中更重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM