i’ve 2 tables. In table_1 i’ve got different tasks that is updated every day. And in table_2 i’ve got list of workers. I need automatically and randomly assign 1 worker(table_2) to 1 task(table_1), so that every worker will have approximately the same amount of tasks.
I’ve tried to use rand() method, it assigns randomly but not equally, i mean one worker can have more tasks to do than another one. then i’ve tried to use count() method, but couldn’t connect them… I’m new in php and mysql 🙁
i made something like this but only in mysql, but it works only once because of (not in) function.
insert into table_1(worker)
select col_1 from table_2
where col_1 not in (select worker from table_1)
order by col_1 rand()
limit 0,1;
Thanks for any help 🙂
I have 2 solutions for you. The first is simpler, but only if you can modify table_2 to add a field for the number of tasks currently assigned. It would be a counter that you would initialize to 0 and then increment every time you assign a task. If you can modify your table, this would select a worker that has the fewest tasks assigned:
This is a round robin assigner, but it isn’t all that random. I would think equitable assignments is more important though. If you can’t alter your tables, the same solution is a bit more messy:
Obviously this isn’t the entire solution. But this will give you a worker_id that has the fewest number of tasks assigned. You’ll then have to use this id to do an update to your task table (table_1)