SELECT with equal chance between same IDs - MySQL How can I SELECT data with equal chances for same IDs in MySQL (for php). For example, assume below data structure:

 id  value
 1   alfa
 1   beta
 1   gamma
 2   hello
 2   bye
 3   sky

I want (alfa, beta, gamma), (hello, bye) and (sky) have the same chances. Here is the probability I need for every row:


alfa: 1/3 * 1/3
beta: 1/3 * 1/3
gamma: 1/3 * 1/3
hello: 1/3 * 1/2
bye: 1/3 * 1/2
sky: 1/3


Thank you.
To select a random id with equal chances:

SELECT DISTINCT id
FROM tableX
ORDER BY RAND()
LIMIT 1 ;


You can combine the above in a subquery (derived table) to find a single row:

SELECT t.*
FROM tableX AS t
  JOIN
  ( SELECT DISTINCT id
    FROM tableX
    ORDER BY RAND()
    LIMIT 1
  ) AS i
    ON i.id = t.id
ORDER BY RAND()
LIMIT 1 ;