I have 3 employers IDs: 1, 2 and 3. I am generating tasks for each one by adding a line in database and in column “for_emp” I insert IDs I want to assign this task for and could be all 3 of them separated by comma. So let’s say I got a task and “for_emp” is “1,2,3”, the employers IDs. If I would like to select all tasks for the ID 2, will I be able to select from the row that has “1,2,3” as IDs and just match “2” there ? If not, how do you suggest I insert my emp IDs into one row in database ? The db is MySQL.
Any ideas ? Thanks.
Don’t do it like that, you should normalize your database.
What you want to do is have a table such as
task, and thentask_assignee.task_assigneewould have fieldstask_idanduser_id. If a task has eg. three assignees (IDs 1, 2 and 3), then you’ll create three rows in thetask_assigneetable for that onetask, like this:Then it’s just a simple matter of querying the
task_assigneetable to find all tasks that are assigned to a given user.Here’s an example of how to get all the tasks for user_id 2:
EDIT.
Just as a related note, even if you didn’t do it the right way (which I described in my answer previously), doing it with hacks such as
LIKEwould still be far from the optimal solution. If you did store a list of comma-separated values, and needed to check if eg. the value 2 is in the list, you could use the MySQL’sFIND_IN_SETfunction:But you shouldn’t do this unless you have no choice (eg. you’re working with someone’s shitty DB design), because it’s way more inefficient and won’t let you index the the employee ID.