I’m checking for existing of a row in in_fmd, and the ISBN I look up can be the ISBN parameter, or another ISBN in a cross-number table that may or may not have a row.
select count(*) from in_fmd i where (description='GN') and ( i.isbn in ( select bwi_isbn from bw_isbn where orig_isbn = ? union all select cast(? as varchar) as isbn ) )
I don’t actually care about the count of the rows, but rather mere existence of at least one row.
This used to be three separate queries, and I squashed it into one, but I think there’s room for more improvement. It’s PostgreSQL 8.1, if it matters.
Why bother with the
UNION ALLI would probably use a
LEFT JOIN-style query instead of theIN, but that’s more personal preference:The inversion discussed over IM: