I have a MySQL database with data in it. They all have locations (London, Liverpool, etc), I want a query to select 10 random rows with a location (for example London), and if there aren’t enough to satisfy the LIMIT request then fill the remaining rows with random other ones, so, for example, 10 random rows requested from London, but I only have 7 with London as the location, so it’ll get the 7 from London and 3 from any other location.
The query is simple:
SELECT * FROM users WHERE active = 'yes' ORDER BY RAND() LIMIT 10
This brings them randomly, and if I put “AND location = ‘$location'” then it gets from the specified location (calling using PHP where $location is a variable stating the priority location), but if there’s less than 10, it’ll only bring those rows, nothing more. I have no idea how to add extra random filler rows if it doesn’t reach 10 at the priority location