Friday 15 July 2011

sql - Select from table only with no records on the second table -



sql - Select from table only with no records on the second table -

i have next tables

table1 (id_owner, name)

1,john 1,elvis 1,michael 1,kelvin

table2 (id_owner, name)

1,kelvin

my conditions are:

only homecoming records table 1 if there isn't records same owner in table 2

but if there records in table_2 must retrieve records table_2

i tried

select coalesce(tb1.name,tb2.name) table_2 tb2 left bring together table_1 tb1 on tb1.owner_id = tb2.owner_id , not exists (select 1 table_2 tb2_sub tb2_sub.owner_id = tb1.owner_id) tb2.owner_id = 1

but when table_2 doesn't have records query doesn't retrieve records.

you overcomplicating code:

select * table1 t1 not exists(select 1 table2 id_owner = t1.id_owner)

sql sql-server

No comments:

Post a Comment