I have a table with an Id column as the primary key, and a number of other
tables that have a foreign key pointing to the first table.
I'm looking for the most efficient/elegant/succinct way to see whether
each row is actually referenced from the other tables.
My first try was:
select distinct a.id, b.id as bid, c.id as cid
from a
left join b on b.id=a.id
left join c on c.id=a.id;
which works, but is fetching a lot of rows from b and c and then discarding
them for the "distinct" qualifier.
My second try was better, but felt rather verbose:
select a.id,
case when exists(select * from b where b.id=a.id) then 1 else 0 end as bid,
case when exists(select * from c where c.id=a.id) then 1 else 0 end as cid
from a;
Is there a better or tidier way to express it?
Cheers
Tony
--
Tony Mountifield
Work:
tony@softins.co.uk -
http://www.softins.co.uk
Play:
tony@mountifield.org -
http://tony.mountifield.org