I am just after some information if anyone knows how to create a system in php that auto assigns a user at random when a record is logged in a mysql database

I have been tasked to build a logging system for which uses a database to store the data but auto assigns users based on a user data database of arround three users to start with, so each time you log a new call is auto assigns a differnet user, this is so each call gets allocated correcly

Does anyone know how I can achieve this


Member Avatar



INSERT INTO table (field1, field2, user_id) VALUES('$field1', '$field2',(SELECT user_id FROM users ORDER BY RAND() LIMIT 1))

However ORDER BY RAND() can be notoriously slow on larger tables.

I wonder why you would use a random assignment if the goal is to get a correct allocation. I'd count the number of assignments for each user and assign the one with the least. To make it simple just add a column to the user table and keep that updated.