If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Self Join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-23-04, 07:28
AbuMariam AbuMariam is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
Red face Self Join

Hi,

I have a table like this

Field 1 Field 2 Field 3
------- -------- --------
1 dsfsdf 1234
2 tgweg 8778
3 dfvsdf 8778
4 wefwe 9938
5 sjdsjn 2398

etc.

I want to select the rows (like 2nd & 3rd ), as it has equal value in the field 3.

please help

thanks
Reply With Quote
  #2 (permalink)  
Old 02-23-04, 07:50
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: Self Join

Your title "self join" suggests that you want to do this:

select t1.field1, t1.field2, t2.field1, t2.field2, t2.field3
from table t1
join table t2 on t1.field2 = t2.field3 and t1.field1 != t2.field1;

However, what if there were 3 or more records with same field3 value?

A more general solution is:

select *
from table
where field3 in
( select field3
from table
group by field3
having count(*) > 1
);
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 02-23-04, 08:31
AbuMariam AbuMariam is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
Many thanks, it is working
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On