Jump to content

Select all but exclude those that match data in a 2nd table from the result


lancia

Recommended Posts

Select table_a.name from table_a where table_b.name = table_a.name & table_b.year = '2009'??

 

 

I'm having trouble explaining the issue, but here goes....

 

table_a

taID | Name

1  | me

2  | you

3  | someone_else

 

table_b

tbID | Name | Year

1    | me    | 2009

2    | someone_else  | 2010

 

 

So my query is:

 

I want to list all names from table_a regardless if they exist in table_b or not (all results), but if they do exist in table_b with the year of '2009' i want to exclude those particular ones from the result.

 

Thanks in advance

Link to comment
Share on other sites

Sorry that i'm not very clear on this...

 

for example if i selected 2010 as the year

>select (all results)table_a.name from table_a exclude table_a.name where table_b.name=table_a.name if table_b.year = '2010'

 

the result should come back as

1  | me

2  | you

as according to table_b '2010' is taken up with 'someone_else' hence not displayed in the result

 

 

if i select 2009 as the year

>select (all results)table_a.name from table_a exclude table_a.name where table_b.name=table_a.name if table_b.year = '2009'

 

the result should come back as

1  | you

2  | someone_else

 

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

Actually Left join ... is null - does not work, After weeks of trial and error this is the correct SQL Statement

 

SOLUTION:

 

Select table_a.name form table_a where NOT EXISTS ((SELECT table_b.name from table_b where table_b.year = '2009') or (table_b.year IS NULL)) order by table_a.name asc

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.