SQL return in different columns, not rows

Asked

Viewed 274 times

4

I am testing a query, where I would need the returns to come in separate columns, but the way below it returns in rows. I tried to use LEFT JOIN but the syntax didn’t work.

select u.nome_completo AS NOME1 from usuarios u where u.cracha = 5357 UNION
select u.nome_completo AS NOME2 from usuarios u where u.cracha = 9999

Return of this query:

inserir a descrição da imagem aqui

Intended return:

inserir a descrição da imagem aqui

  • 1

    gives a search for Pivot Tables (http://stackoverflow.com/questions/7674786/mysql-pivot-table)

  • @Marciano.Andrade had seen something about pivot Ables, but in my case it is not count, it would return the same query. I’m trying to do with left Join but it’s bone. Thank you so much for the tip.

1 answer

2


Doing the Pivot "in hand"

select max(nome1) nome1 , max(nome2) nome2
from
(
select u.nome_completo AS NOME1 , '' as nome2 from usuarios u where u.cracha = 5357 
UNION
select '' as nome1 , u.nome_completo AS NOME2 from usuarios u where u.cracha = 9999
) virtual
  • Good, that was it. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.