Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Encontre a melhor linha correspondente no MySQL (InnoDB)


Basta usar LIKE, mas ao contrário do que você provavelmente costumava usar.
select query
from table1
where 'engine/mail/key' like concat(query,'%')
order by length(query) desc
limit 1

Resultados:
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)