0

Hello StackOverflow Community,

I have question regarding the best method (in terms of resources/speed/etc.) to find where a defined valued is in an imploded string that is stored in a cell in the database.

ie: (all elements in table are either INT or STRING)

id | age | name | groups
=========================
0  | 28  | bob  | 1,2,3,4
1  | 33  | sara | 1,4

Given the requested group would be 2, Would it be best to select all users and iterate through their groups manually to see if it matches to 2. (which doesn't sound resourceful at all)

or is there a way to do a constant IN array query via MySQL

SELECT * FROM tableUsers WHERE 2 IN groups

which would select user id with 0.

^ obviously that won't work but is there a similar alternative?

Jake Dev
  • 19
  • 6
  • Yikes! You really should consider how you're saving this data. It will give you many headaches later on. – Jay Blanchard Feb 08 '16 at 20:10
  • 1
    @JayBlanchard Looks like it's not possible/feasable. I'll have to get back to the client to restructure the database. – Jake Dev Feb 08 '16 at 20:13

1 Answers1

0

its easy. You can do this:

SELECT * FROM tableUsers WHERE FIND_IN_SET(2,groups);
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39