Re: SQL query...

From: greg(at)turnstep(dot)com
To: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL query...
Date: 2003-02-19 14:31:56
Message-ID: 37e29b3d0c58bc8199291d16782cc692@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> if i have 5 tables and all of it has column X and i wanted to get unique
> values in column X for all tables..
> can i do it in one query?

Yes, you want to use UNION:

SELECT foo FROM table1
UNION
SELECT foo FROM table2
UNION
SELECT foo FROM table3
UNION
SELECT foo FROM table4
UNION
SELECT foo FROM table5;

This will by default remove all duplicate entries. Use UNION ALL to
see all entries, even duplicated ones. Note that UNION (and its
companions INTERSECT and EXPECT) all require that the tables have
the same number of columns, and that each column is of a compatible
type.

- --
Greg Sabino Mullane greg(at)turnstep(dot)com
PGP Key: 0x14964AC8 200302190926

-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html

iD8DBQE+U5SSvJuQZxSWSsgRApdKAJ4oV4G1nq9RVoNbNX4qJ8hy/fI4eQCfTqcE
r3xW8sPVsIdwotPxoZHUHGk=
=P8cL
-----END PGP SIGNATURE-----

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-02-19 14:44:03 Re: Index not used with IS NULL
Previous Message Martijn van Oosterhout 2003-02-19 14:22:17 Re: 7.3.1 takes long time to vacuum table?