Search code examples
phpmysqlsql-like

PHP MySql Like Query to Match Int in String


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.


Solution

  • Try using FIND_IN_SET:

    $query = "SELECT * FROM $table WHERE FIND_IN_SET('$match_value', $match_column);"