How to search a MySQL table for the most accurate matching -


i have table contains row of phone number prefixes , row of price each prefix.

sample table:

   prefix  |  price  --------------------- |  21366   |   0.15   | |  2010    |   0.1    | |  213     |   0.13   |  --------------------- 

in website, user asked insert phone number (e.g. 21366123456), have search database table accurate matching.

in case, though there prefix value of 213 in table, needing result of 21366.

how can done using simple mysql query without looping recursively on user input value?

try this:

select prefix, price mytable order length('21366123456') - length(replace('21366123456', prefix, '')) desc limit 1 

demo here


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -