| From: | David Rowley <david(dot)rowley(at)2ndquadrant(dot)com> |
|---|---|
| To: | Patrick B <patrickbakerbr(at)gmail(dot)com> |
| Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
| Subject: | Re: Select from tableA - if not exists then tableB |
| Date: | 2017-05-08 22:08:21 |
| Message-ID: | CAKJS1f9ZrSc5soC4nZphommYd0VpG5YoZ3eXU_oGAPU+x=v27Q@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
On 9 May 2017 at 09:56, Patrick B <patrickbakerbr(at)gmail(dot)com> wrote:
> Hi guys,
>
> I have two tables that supports the same data, but different table DDL (We
> are migrating all the data from one to another).
>
> What I need is basically:
>
> 1. Query looks for the data on table A,
> 2. if it doesn't find it on table A, go look for it on table B
>
> Now, how could I do that in a Select? Can you please provide some examples?
>
>
> I'm using PostgreSQL 9.1.
You could exploit DISTINCT ON for this.
Something like:
select distinct on (id) id,value from (select *,'a' tablename from a
where id=1 union all select *,'b' tablename from b where id=1) ab
order by id,tablename;
Assuming that id is what you want to be unique.
--
David Rowley http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Brian Dunavant | 2017-05-08 22:19:16 | Re: Select from tableA - if not exists then tableB |
| Previous Message | Adrian Klaver | 2017-05-08 22:01:32 | Re: Select from tableA - if not exists then tableB |