Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
Query using RAND but following a defined structure

I have the following problem and I would like to know how to solve it creating a query on MySQL.

Check out those two tables:

enter image description here

enter image description here

I need to follow a structure to show the results I want. It is like a randon inside a randon. For example, I want to randomize each section, but ids 1 to 3 must be together and then randomize too.

enter image description here

How would be a query to get such results?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

With this query:

select id, id_start, id_end, rand() rnd
from table2
group by id, id_start, id_end

you can return a random number for each of the rows of table2.
Join this query to table1 and sort the result first by that random number and then by random:

select t1.id, t1.description
from table1 t1 
inner join (
  select id, id_start, id_end, rand() rnd
  from table2
  group by id, id_start, id_end
) t2
on t1.id between t2.id_start and t2.id_end
order by t2.rnd, rand()

See the demo.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda