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

237
Visualizações
Insert serial no in mysql union query

I am having mysql query using union as

(Select id,c1,c2,c3 from table where c1=x1 and c2=x2 ) union  <<---block 1
(Select id,c1,c2,c3 from table where c1=x1 and c2=x2 ) union  <<---block 2
...
(Select id,c1,c2,c3 from table where c1=x1 and c2=x2 ) union  <<---block mth

which returns result

id|c1|c2|c3
------------
1 |..|..|..
2 |..|..|..
3 |..|..|..

.. .. .. ..
.. .. .. ..
nth.. .. ..

I want table which adds a no showing the result is obtained from which block of union

id|c1|c2|c3|blk
---------------
1 |..|..|..|1
2 |..|..|..|1
3 |..|..|..|2

.. .. .. ..|3
.. .. .. ..|3
nth.. .. ..|3
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Add 1 more column to each query with the blk number:

(Select id,c1,c2,c3, 1 blk from table where c1=x1 and c2=x2 )
(Select id,c1,c2,c3, 2 from table where c1=x1 and c2=x2 ) 
...
(Select id,c1,c2,c3, <n> from table where c1=x1 and c2=x2 )
over 4 years ago · Santiago Trujillo Relatório

0

Just add a constant to the select clause:

SELECT id, c1, c2, c3, 1 AS blk FROM table WHERE c1 = x1 AND c2 = x2 UNION ALL -- block 1
SELECT id, c1, c2, c3, 2 FROM table WHERE c1 = x1 AND c2 = x2 UNION ALL        -- block 2
...
SELECT id, c1, c2, c3, m FROM table WHERE c1 = x1 AND c2 = x2                  -- block m
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