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 > Join Problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-01-05, 07:32
Nura Nura is offline
Registered User
 
Join Date: Oct 2005
Posts: 21
Join Problem

HI
I have got 3 tables
Member_master(mem_id,mem_name)
Member_address(mem_id,mem_add1,mem_add2)
Member_relatives(mem_id,rel_name,rel_add1,rel_add2 )

A member has multiple rows in Member_address and multiple rows in Member_relatives.The problem is that the address values are repeated.
i used join but there was repetion of similar rows.
How can i retrieve data from these tables without the repetion of the address.
Reply With Quote
  #2 (permalink)  
Old 10-01-05, 08:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you have two unrelated (unrelated to each other) one-to-many relationships

if you do a three-table join, then of course you will get "duplicates"

the only way that you can retrieve data from both one-to-many relationships at the same time in the same query is with a UNION, like this --
Code:
select M.mem_id
     , M.mem_name
     , 'member address: ' as address_type
     , A.mem_add1         as address_1
     , A.mem_add2         as address_2
  from Member_master as M
inner
  join Member_address as A
    on M.mem_id = A.mem_id
union all
select M.mem_id
     , M.mem_name
     , 'relative address: ' 
     , R.rel_add1
     , R.rel_add2
  from Member_master as M
inner
  join Member_relatives as R
    on M.mem_id = R.mem_id
note it's UNION ALL, not UNION
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-03-05, 00:13
Nura Nura is offline
Registered User
 
Join Date: Oct 2005
Posts: 21
It did work thanks
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