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

$query = $this->select()
    ->from($this->_name)
    ->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.

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

Best to dump it to a string and parse it using php functions.

Member Avatar for iamthwee

Edit*

Maybe you could use the LIKE command.

Use FIND_IN_SET()

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.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.