Re: Join question - Mailing list pgsql-general

From Jeffrey Melloy
Subject Re: Join question
Date
Msg-id 1A269922-D9E2-11D7-8AF9-000393C78AC0@visualdistortion.org
Whole thread Raw
In response to Re: Join question  ("Williams, Travis L, NEO" <tlw@att.com>)
List pgsql-general
On Thursday, August 28, 2003, at 09:03  PM, Williams, Travis L, NEO
wrote:
>> I have a table1 with 2 col (a & b) where b can sometimes be null. I
>> need a query that if B is null I get back the contents of A.. but if B
>> is not null I do a "select d from table2 where d like '%b%'"  There is
>> nothing to join between table1 & table2 (unless you can join on likes
>>

You can do something like this, but I can't promise any great
performance:

select case when b is null
                      then a
                      else (select d from table2 where d ~* b) end as
value
from    table1;

jmelloy=# select * from table1;
  a |  b
---+------
  1 |
  2 |
  3 |
  4 | for
  5 | asdf
  6 | coo
(6 rows)

jmelloy=# select * from table2;
     d
----------
  forsythe
  manasdf
  cool
(3 rows)

jmelloy=# select case when b is null then a::varchar else
jmelloy-# (select d from table2 where d ~* b) end as value
jmelloy-# from table1;
   value
----------
  1
  2
  3
  forsythe
  manasdf
  cool
(6 rows)


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: left outer join terrible slow compared to inner join
Next
From: "Williams, Travis L, NEO"
Date:
Subject: Re: Join question