Mysql
 sql >> Database >  >> RDS >> Mysql

Come posso ottenere una distribuzione uniforme usando WHERE id IN(1,2,3,4)

Impostando il numero di riga con le variabili, quindi filtrando il risultato per mostrare solo la riga 1-3 dovrebbe funzionare

SET @last_objectId = 0;
SET @count_objectId = 0;
SELECT id, name, image FROM (
SELECT
 user.id,
 user.name,
 user.image,
 @count_objectId := IF(@last_objectId = rating.objectId, @count_objectId, 0) + 1 AS rating_row_number,
 @last_objectId := rating.objectId
FROM users
LEFT JOIN ratings ON (ratings.userid = user.id)
WHERE
 rating.rating > 0 AND
 rating.objectId IN (1,2,3,4)
ORDER BY rating.objectId
) AS subquery WHERE rating_row_number <= 3;