Find best matching row in MySQL (InnoDB)

后端 未结 1 1116
梦如初夏
梦如初夏 2021-01-04 13:30

I have the following test string engine/mail/key and a table which looks like this:

+-------------+
| query       |
+-------------+
| engine            


        
相关标签:
1条回答
  • 2021-01-04 14:14

    Just use LIKE, but the other way around to what your probably used to.

    select query
    from table1
    where 'engine/mail/key' like concat(query,'%')
    order by length(query) desc
    limit 1
    

    Results:

    mysql> select * from query;
    +-------------+
    | query       |
    +-------------+
    | engine      | 
    | engine/pdf  | 
    | engine/mail | 
    +-------------+
    3 rows in set (0.00 sec)
    
    mysql> select query from query 
           where 'engine/mail/key' like concat(query,'%') 
           order by length(query) desc 
           limit 1;
    +-------------+
    | query       |
    +-------------+
    | engine/mail | 
    +-------------+
    1 row in set (0.01 sec)
    
    0 讨论(0)
提交回复
热议问题