Hi. I have a mysql field with a record like: [2,5,11].
I have a query like:

$query = $this->select()
    ->where('id = ?', $id);

This query work if record is of integer type: 5.
Is it possible to create a query to find 5 in the array? Please advise. Thanks.

3 Years
Discussion Span
Last Post by eburlea

Thank you all for reply.

I think it is not possible to use LIKE here because there may be found situation of [12,32] will return true when searching for 2.

I have tried FIND_IN_SET(). The problem is that it is working without '[]', but I need these square brackets.


Finally I've got the solution.

->where("FIND_IN_SET($id, TRIM(TRAILING ']' FROM (TRIM(LEADING '[' FROM `id`))))");
  1. First I get rid of the leading '['
  2. Then I get rid of the ']' from the end of the string
  3. After that I can use FIND_IN_SET to check if my id exists there.

Edited by eburlea: Spelling

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.