Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda