0

In MySQL, I am trying to find rows with values matching the pattern. In my data, the pattern that I am using is 401.%, 402.%, 403.%, 404.% and 405.%. In other words, I am trying to find any floating number between 401 and 405. In addition, these number is stored as varchar. It is a string. So far, I have tried the following code, but in vain:

select * from my_table where my_column like '401.%'

and so forth.

Could you think of a way to make this work? Thanks for your help in advance.

3
  • So how should the LIKE operator treat [1-5]? It searches for this exact string and since you don't have one - it fails to return anything. Commented Mar 26, 2014 at 2:50
  • 1
    What you have there will only work with my_column REGEXP '^40[1-5]\.' Commented Mar 26, 2014 at 3:10
  • I was not clear about the intention by posting my example code (edited). I was looking for something like regular expression. I wanted to avoid repeating the like statement five times. Commented Mar 26, 2014 at 15:09

2 Answers 2

2
select * from my_table where my_column like '401.%' or my_column like '402.%' or my_column like '403.%' or my_column like '404.%' or my_column like '405.%'

Should perfrom best by far ... sort of loop unrolling in SQL

Sign up to request clarification or add additional context in comments.

3 Comments

Do you actually know if this query will use an index?
Just tried a quick&dirty on my localhost (Ubuntu 12.04, MySQL 5.5.35-0ubuntu0.12.04.2) and execution plan says "possible keys" but not "key" ... but only 1000 rows and all in cache.
@GordonLinoff Yes, it will use index; just tested with >= 300k records on 5.5 :)
2

MySQL is very nice about converting numbers to string. So, you can try something like this:

where my_column + 0 >= 401 and my_column < 406

Of course, this doesn't handle situations where bad characters occur after the first three characters. But it might suffice for your purposes.

EDIT:

If you actually wanted to use an index for this, then do:

where my_column >= '401' and my_column < '406' and
      my_column + 0 >= 401 and my_column < 406

The first two string comparisons should take advantage of an index.

5 Comments

as much as I hate disagreeing with somebody of your reputation, but have you thought about waht this means? Casting ALL records to numbers - even those which will not match and forfeiting all index use?
@EugenRieck . . . Possibly a good point, but I'm not so sure that MySQL will use an index for a string of like comparisons connected by or.
If the LIKE does NOT start with a wildcard, the index will be used (if it exists). In this case, the ORs will be executed one after the other.
@EugenRieck . . . I can't speak to this specific instance, but I have at times had to use union all to get multiple conditions connected by or's to use indexes.
Your edited answer looks like it combines all the good stuff - the OP should accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.