sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo agregar pesos a una tabla MySQL y seleccionar valores aleatorios de acuerdo con estos?

Encontré este buen algoritmo en Quod Libet. Probablemente podría traducirlo a algún SQL de procedimiento.

function WeightedShuffle(list of items with weights):
  max_score ← the sum of every item’s weight
  choice ← random number in the range [0, max_score)
  current ← 0
  for each item (i, weight) in items:  
    current ← current + weight  
    if current ≥ choice or i is the last item:  
      return item i