Solved

Data from table1 where no match on Table2

Posted on 2009-05-15
8
334 Views
Last Modified: 2012-05-07
I would like to write a query which bring back all the data from table1 where there is match in table2.

The match uses a primary key called TableJoinKey

can anybody help.

Thanks
0
Comment
Question by:Mr_Shaw
  • 4
  • 3
8 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 24393676
simply join:
select t1.*
  from table1 t1
 where exists ( select null from table2 t2 where t2.TableJoinKey = t1.tableJoinkey ) 

Open in new window

0
 

Author Comment

by:Mr_Shaw
ID: 24393714
what would happen if i used a left or right join
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24393796
you can use this one also.

select t1.* from table1 t1 left join table2 t2 on t1. TableJoinKey  =t2. TableJoinKey  where t2.fieldone=NULL
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24393806
above query will return only records not match with table2, if you want records with match, use this one.


select t1.* from table1 t1 left join table2 t2 on t1. TableJoinKey  =t2. TableJoinKey  where t2.fieldone is not NULL
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24393839
you can use AngelIII's solution also,

for records match:

select t1.*
  from table1 t1
 where exists ( select null from table2 t2 where t2.TableJoinKey = t1.tableJoinkey )


for records not match:

select t1.*
  from table1 t1
 where not exists ( select null from table2 t2 where t2.TableJoinKey = t1.tableJoinkey )
0
 

Author Comment

by:Mr_Shaw
ID: 24393856
ok i'll have a play arround...

Please could someone tell me what would happen if I used a left or right join.
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24393877
if you use left join than you will get all records from table 1 and records doesn't match with table1, will come as NULL in table2. have a look at following query.

select t1.* from table1 t1 left join table2 t2 on t1. TableJoinKey  =t2. TableJoinKey

reverse process than above if you use right join.
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31581849
Thanks
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now