Searching for MySQL-Statement...

Posted by: rowitech

Searching for MySQL-Statement... - 21/06/2005 09:00

Hi,

I want to search for a matching number in a database.
It should match only from the beginning, not inside the string.
My problem is that I cannot just add a ^ sign before the "pattern" because it's the name of a field in the database.


Example:
Code:
mysql> SELECT * FROM routes WHERE "49210491" RLIKE pattern ORDER BY LENGTH(pattern) DESC;


+---------+----------------------+---------+-------------+-----------------+--------+
| pattern | comment | trunks | connectcost | includedseconds | cost |
+---------+----------------------+---------+-------------+-----------------+--------+
| 491 | Sondernummern | | 0 | 0 | 900000 |
| 49 | Festnetz Deutschland | nikotel | 0 | 0 | 200 |
+---------+----------------------+---------+-------------+-----------------+--------+



I want the function to give me the longest matching pattern. Currently it gives back 491also due to the pattern _inside_ the string. In this example best match would be just 49.

Hope you know what I'm trying to say.

Rolf

edit : Formatted text
--l0ser
Posted by: wfaulk

Re: Searching for MySQL-Statement... - 21/06/2005 17:41

Would
Code:
WHERE "49210491" RLIKE CONCAT("^", pattern)

work?
Posted by: rowitech

Re: Searching for MySQL-Statement... - 22/06/2005 03:43

Perfect!

That's exactly what I searched for. I didn't now that concat even works this way, I'm surprised.. Thank you very much.

regards
Rolf