Skip site navigation (1) Skip section navigation (2)

Re: SELECT from two tables with different field names?

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: SELECT from two tables with different field names?
Date: 2011-12-12 08:25:06
Message-ID: jc4dn7$m06$1@dough.gmane.org (view raw or flat)
Thread:
Lists: pgsql-novice
Pandu Poluan, 12.12.2011 05:39:
> Hello!
>
> Due to some legacy apps, I end up with two tables with similar information, but with zero intersection (i.e., both tables are exclusive of each other).
>
> For illustration:
>
> table_one has the fields emp_id and fullname
>
> table_two has the fields employee_id, first_name, and last_name
>
> Now, I need a query that will search for employee ID in both tables and return his/her full name.
>
> How do I do that? Will a simple UNION suffice?

Yes a UNION should do (actually a UNION ALL as it will not try to remove duplicates which makes the query faster)

select *
from (
    select emp_id, fullname
    from table_one

    union all

    select employee_id,
           first_name||' '||last_name
    from table_two
) t
where emp_id = 1




In response to

Responses

pgsql-novice by date

Next:From: Jean-Yves F. BarbierDate: 2011-12-12 18:10:53
Subject: index or not
Previous:From: Pandu PoluanDate: 2011-12-12 04:39:19
Subject: SELECT from two tables with different field names?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group