Admin posted on September 15, 2009 08:59

Uma dica rápida, mas que pode poupar um bom tempinho. Usar ORDER BY em queries que usam a cláusula UNION é bem mais simples do que parece. Suponhamos que temos duas tabelas, tb_cidades e tb_paises, e queremos trazer em uma única query o id e o nome tanto de tb_cidades quanto de tb_paises. Para isso combinamos duas queries SELECT usando a cláusula UNION:

SELECT c.cidade_id, c.nome FROM tb_cidades c
UNION
SELECT p.pais_id, p.nome FROM tb_paises p

Teremos assim, em uma única query, tanto cidades quanto países. Mas, e a ordenação?? É aí que muita gente se atrapalha, mas a solução é bem simples:

1) Atribua um alias em comum para as colunas que serão ordenadas. Se for a coluna de ID, coloque o mesmo alias tanto para cidades quanto para países.

2) Utilize este alias no ORDER BY.

Então vamos alterar a query acima para ordenar pela coluna nome de tb_cidades e tb_paises:

SELECT c.cidade_id, c.nome AS coluna_nome FROM tb_cidades c
UNION
SELECT p.pais_id, p.nome AS coluna_nome FROM tb_paises p
ORDER BY coluna_nome

Simples assim. Wink


Posted in: PostgreSQL  Tags: , ,

Comments


October 27. 2009 15:07
Vince Black Stretch Knit Pants
Vince Black Stretch Knit Pants

http://www.chavsnextdoor.info/Vince-Black-Stretch-Knit-Pants.phphttp://www.chavsnextdoor.info/Vince-Black-Stretch-Knit-Pants.php


November 3. 2009 11:06
Zoloft
Hope youare going to write more posts for my mood elevation.

http://www.24med.net/http://www.24med.net/


December 22. 2009 12:21
Jiin
Considerably, the article is in reality the greatest on this noteworthy topic. I agree with your conclusions and will eagerly look forward to your next updates. Saying thanks will not just be sufficient, for the wonderful clarity in your writing. I will immediately grab your rss feed to stay privy of any updates. Pleasant work and much success in your business dealings!

http://lossweightdiets.com/acai-berry-benefits-you-can-take-off-the-weight/http://lossweightdiets.com/acai-berry-benefits-you-can-take-off-the-weight/


January 3. 2010 23:49
kamagra 100
Thanks for sharing such a nice information..
If you have any type of sexual problem you can go with noline pharmacy like http://www.kamagrarx.com.
You can alo use generic viagra,meltabs,kamagra,silagra,edegra etc.but only after concerning with doctor.

http://www.kamagrarx.com/http://www.kamagrarx.com/


February 9. 2010 12:16
quick payday loans
I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

http://www.globalpaydayloans.com/http://www.globalpaydayloans.com/


February 20. 2010 11:08
splendid ballet cami dress
Nice blog - I will link to it from my splendid ballet cami dress website.

http://www.chaikendress.info/splendid-ballet-cami-dress.phphttp://www.chaikendress.info/splendid-ballet-cami-dress.php


February 25. 2010 15:25
payday loans
I just hope to have understood this the way it was meant

http://www.sonicloans.net/http://www.sonicloans.net/


February 27. 2010 09:49
cash advance
Your blog is so informative … keep up the good work!!!!

http://www.flyingloans.com/http://www.flyingloans.com/


March 7. 2010 06:38
nanette lepore elle magazine
i run a wordpress blog about nanette lepore elle magazine, I think I will change to this blogengine system as it is a lot nicer than mine.

http://www.ecoganik.info/nanette-lepore-elle-magazine.phphttp://www.ecoganik.info/nanette-lepore-elle-magazine.php

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Páginas

Calendário

«  March 2010  »
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234
View posts in large calendar