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

Select from second table only if select from first returns no results

From: Petros Thespis <pthespis(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Select from second table only if select from first returns no results
Date: 2009-07-28 11:15:42
Message-ID: 3762cdb90907280415l62abc07xeb3b68a356a72542@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-novice
Hello all,

I'm facing the following problem.

I have a database with two tables, t1 and t2. The two tables have the same
simple structure, say columns col1, col2. What I want to do is to select
from t1 and, in case no results are returned, to then select from t2.

Right now, all I'm doing is

SELECT col1 FROM t1 WHERE col2 = "STH"
UNION
SELECT col1 FROM t2 WHERE col2 = "STH";

That is, as far as I know, I always check both t1 and t2 and, moreover, I
get no guarrantee that t1 entries will come first in the list of the
results.

Any ideas on how to solve this?

P. Thespis

Responses

pgsql-novice by date

Next:From: A. KretschmerDate: 2009-07-28 11:30:29
Subject: Re: Select from second table only if select from first returns no results
Previous:From: Andreas WenkDate: 2009-07-28 09:56:42
Subject: Re: Location of databases

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