Re: select union with table name

From: Reece Hart <reece(at)harts(dot)net>
To: Akbar <akbarhome(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: select union with table name
Date: 2006-12-29 05:03:49
Message-ID: 1167368629.26596.3.camel@snafu.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 2006-12-28 at 19:09 +0700, Akbar wrote:
> select blue.name from blue union select red.name from red
> give me this:
> name
> 'blabla'
> 'bubu'
> 'haha'
> 'kkk'
>
> I want this:
> name table_name
> 'blabla' blue
> 'bubu' blue
> 'haha' red
> 'kkk' red
>
> Could I?

select name,'blue' as "table_name" from blue union all select name,'red'
as "table_name" from red;

Note the 'all' after union... I suspect you'll want that or should at
least consider it.

-Reece

--
Reece Hart, http://harts.net/reece/, GPG:0x25EC91A0
./universe -G 6.672e-11 -e 1.602e-19 -protonmass 1.673e-27 -uspres bush
kernel warning: universe consuming too many resources. Killing.
universe killed due to catastrophic leadership. Try -uspres carter.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ben 2006-12-29 07:11:47 Re: Geographical redundancy
Previous Message Tom Lane 2006-12-29 04:44:30 Re: questions about migrating form postgres 7 to 8