Home Ask Login Register

Developers Planet

Your answer is one click away!

Jake Dev February 2016

MySQL select where constant is in array in table

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?

Answers


Bernd Buffen February 2016

its easy. You can do this:

SELECT * FROM tableUsers WHERE FIND_IN_SET(2,groups);

Post Status

Asked in February 2016
Viewed 1,479 times
Voted 13
Answered 1 times

Search




Leave an answer


Quote of the day: live life