I have a Mysql database table with ~10000 songs. One column is the last time they've been played. I want to randomly select a song, giving more weight to the least played songs. It should be a fast query, so maybe we don't need to count the number of times a song's been played and just use the 'last_play' column to calculate the weight.
I'm using PHP and Mysql and I'm not sure if I can do the query with just SQL or if I need to get all 10.000 results and then make calculations using PHP.
Could you please help me with the query? Maybe there is a better approach to solve the problem?
The table:
CREATE TABLE `songs` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT '0',
`filename` VARCHAR(150) NULL DEFAULT '0',
`last_play` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
;