?
Solved

Data from table1 where no match on Table2

Posted on 2009-05-15
8
Medium Priority
?
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 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 1600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 1600 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
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 1600 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 1600 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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

770 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