1

MySQL Table video

id | path
4  | 89_1232
6  | 90_2121

I need to match path before underscore _. For instance 89 , 90. Is it possible with MySQL query.

Right now what I am doing is, fetching all rows and then explode path row:

"SELECT `path` FROM `video`

then foreach () { explode and check }

2
  • 1
    path LIKE "%' . $value . '_" Commented Nov 10, 2015 at 12:38
  • I'm worried you have put two separate pieces of data in one column, consider separating these out into two columns, then you can index and match all the parts independently.. you can always concat on select if you need the original display. Commented Nov 10, 2015 at 12:44

1 Answer 1

3

Use LIKE with wildcards

Example:

SELECT `path` FROM `video` WHERE `path` LIKE '89\_%' ESCAPE '\'

which will search for rows with the pattern 89_{anything}, using the backslash \ as the escape character (to make sure the underscore _ is literal)

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

2 Comments

Actually underscore means one of any character unless escaped.
I'd also point out, that as this is the left most portion of the string, it can still use an index on the column.. which is why I'd use this method over SUBSTRING_INDEX.

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.