开发者

SQL string match numbers, varying length

I am looking up which exchange services which telephone numbers, from a table of fragmentary numbers that show which exchange services them. So my table contains, for example:

id      |exchcode   |开发者_Go百科exchname       |easting|northin|leadin     |
-----------------------------------------------------------------
12122   |SNL/UC     |SANDAL        |43430   |41306  |1924240    |
12123   |SNL/UC     |SANDAL        |43430   |41306  |1924241    |
881     |SNL/UD     |SANDAL      |43430 |41306  |1924249    |
2456    |BD/BCC/1   |BRADFORD CABLE |41627  |43262  |192421     |
4313    |NEY/UB     |NORMANTON      |43847  |42289  |192422     |
12124   |SNL/UC     |SANDAL         |43430  |41306  |192425     |
9949    |OBE/UB     |HORBURY OSSETT  |42857 |41971  |192428     |
9987    |OBE/UB     |WAKEFIELD       |42857 |41971  |1924       |

(sorry, formatting a bit rubbish)

leadin is the leading part of the phone number I have to match (stored as a VARCHAR, not a number)

And I am supplied with a phone number 1924283777 (not real) how do I query to get the best match from the above table (It should pick exchange id 9949), or do I deal with it in code after I've done the query (php)

tl;dr: variable length for values of leadin column, want best match with a number longer than leadin.


I would think something like

WHERE ? LIKE concat(leadin, '%') order by length(leadin) desc limit 1

(I haven't checked the function names, and I'm not certain that this will work in MYSQL - I'm pretty sure it will work in one of the SQL's I've used).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜