0

I am trying to do what seems to be a simple MySQL query but cannot get it to work.

$query = "SELECT * FROM $table WHERE $match_column LIKE CONCAT('%',CAST($match_value AS CHAR),'%') ; <br />

The data in $match_column is a string of integers separated by a comma like this: 1,3,13,2. The data type in the table is varchar. If my search term is any one of those integers, such as 13, I want to include that row in result but it doesn't work. The query string does work on data with a single integer in $match_column. Using regEx might be the answer, but I am nor proficient there. Any help will be appreciated.

2
  • What results are you seeing? None at all? On another note, keep in mind that you need to make sure "1" doesn't match "11,12,13"; your current code would have this issue even if it was working the way you intended. Commented Jun 10, 2013 at 21:01
  • You may want to look at FIND_IN_SET: dev.mysql.com/doc/refman/5.0/en/string-functions.html Commented Jun 10, 2013 at 21:18

1 Answer 1

1

Try using FIND_IN_SET:

$query = "SELECT * FROM $table WHERE FIND_IN_SET('$match_value', $match_column);"
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm. I guess I'm rustier than I thought on my MySQL! Thanks.

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.