MySqlTalk.com  

Go Back   MySqlTalk.com > MySQL > General
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread
Old 09-20-2005, 06:22 PM   #1
radox
Junior Member
 
Join Date: Nov 2004
Posts: 13 radox is on a distinguished road
Weird Join...Help Please

I don't even know if this is possible, but here it goes.

I have two tables.

the first one (contacts) contains three columns (id,firstname, lastname)
the second (members) contains two columns (id1, id2) both are foreign keys to (contacts)

how do i produce one join to give me both firstname and lastname columns?

the below select doesn't work, just trying to show my thinking:

Code:
SELECT firstname,lastname,firstname,lastname FROM contacts INNER JOIN members ON contacts.id=members.id1 INNER JOIN members ON contacts.id=members.id2

I'm I making any sense? Is this even possible?
There are obviously more columns to my tables. Getting this info would get my answer though.

Thanks in advance.
radox is offline   Fork this post Reply With Quote
Old 09-20-2005, 08:21 PM   #2
Azkaban
Senior Member
 
Join Date: Jul 2004
Posts: 167 Azkaban is on a distinguished road
Re: Weird Join...Help Please

It might help if you provide an explanation of what you're really trying to do, in other words WHY you want firstname and lastname twice per line. There could be another way to do this.

I always found self joins confusing, so I may not be able to help you. But I'll try if you can explain better too.

A couple tips though. When I've seen self joins there's always a renaming of the table like

SELECT t1.x,t2.x FROM mytable AS t1, mytable as t2

In the join

FROM member as m1
FROM member as m2

etc......

I also doubt you are putting the join together correctly. You'll need to reference the member in the FROM rather than the join...again, I believe.
Azkaban is offline   Fork this post Reply With Quote
Old 09-20-2005, 09:34 PM   #3
radox
Junior Member
 
Join Date: Nov 2004
Posts: 13 radox is on a distinguished road
Re: Weird Join...Help Please

thanks for you reply.

id1 and id2 are both foreign keys to the (contacts) table.

the (firstname) for id1 will be different than the (firstname) of id2

i can easily join id1 by:

Code:
SELECT firstname,lastname FROM members INNER JOIN contacts ON members.id1=contacts.id

that will give me firstname and lastname for id1

i need to pull from the same table again and get firstname and lastname for id2

i'm trying to get this done in one SELECT statement....

maybe it can't be done??
radox is offline   Fork this post Reply With Quote
Old 09-20-2005, 09:47 PM   #4
Azkaban
Senior Member
 
Join Date: Jul 2004
Posts: 167 Azkaban is on a distinguished road
Re: Weird Join...Help Please

I think it can be done even though I don't have the answer on my tongue. But I'm still trying to understand why you need it. You don't need to give me the real example if this is a private project. But any example doing the same type of thing would probably help me come up with a solution for you. Helps me jog the thinking when I can apply the situation to the real world.
Azkaban is offline   Fork this post Reply With Quote
Old 09-20-2005, 10:00 PM   #5
radox
Junior Member
 
Join Date: Nov 2004
Posts: 13 radox is on a distinguished road
Re: Weird Join...Help Please

gotcha...

ok, here's the real example.

this is for a mortgage company...i just tried to generalize the example.

there is a table for each borrower file.
there is a table for all our investors.

there can be one investor on the 1st mortgage and a different one for the 2nd mortgage.

the borrower table has a integer column (firstmort) and integer columun (secmort)

i need to join these int columns to get the names of the investors.

did i confuse this more or make some sense?
radox is offline   Fork this post Reply With Quote
Old 09-21-2005, 01:14 AM   #6
Azkaban
Senior Member
 
Join Date: Jul 2004
Posts: 167 Azkaban is on a distinguished road
Re: Weird Join...Help Please

That helps a lot. First of all, I find it easier to visualize this by being more descriptive when naming the id's. Instead of just "id" in the contacts table, how about contactid?

Second, you are missing a primary key in the second table. Rather than id1 and id2, make it memberid,investorid1,investorid2.

Then try this (untested, my syntax may be slightly off, but this concept should do the trick):

SELECT t1.firstname as f1,t1.lastname as l1,t2.firstname as f2,t2.lastname as l2
FROM contacts as t1,contacts as t2
LEFT JOIN members ON t1.contactid=members.investorid1
LEFT JOIN members ON t2.contactid=members.investorid2
Azkaban is offline   Fork this post Reply With Quote
Old 09-21-2005, 11:47 AM   #7
radox
Junior Member
 
Join Date: Nov 2004
Posts: 13 radox is on a distinguished road
Re: Weird Join...Help Please

Concept looks good. I tried every possible variation. Looks like joining to the same table twice is not allowed. i keep getting "not unique table/alias"
radox is offline   Fork this post Reply With Quote
Old 09-21-2005, 12:18 PM   #8
radox
Junior Member
 
Join Date: Nov 2004
Posts: 13 radox is on a distinguished road
Re: Weird Join...Help Please

I got it. Need to alias the tables as well.

SELECT t1.firstname as f1,t1.lastname as l1,t2.firstname as f2,t2.lastname as l2
FROM contacts as t1,contacts as t2
LEFT JOIN members AS m1 ON t1.contactid=m1.investorid1
LEFT JOIN members AS m2 ON t2.contactid=m2.investorid2

Tricky Sh!t...

Thanks so much for you help.
radox is offline   Fork this post Reply With Quote
Old 09-21-2005, 07:06 PM   #9
radox
Junior Member
 
Join Date: Nov 2004
Posts: 13 radox is on a distinguished road
Re: Weird Join...Help Please



This only works for one record...
When there is more than one record, the results go up by that much.

In other words:

1 rows x 1 = 1 results
2 rows x 2 = 4 results
3 rows x 3 = 9 results

I don't know how to use GROUP BY correctly


This is so frustrating
radox is offline   Fork this post Reply With Quote
Old 09-21-2005, 07:27 PM   #10
Azkaban
Senior Member
 
Join Date: Jul 2004
Posts: 167 Azkaban is on a distinguished road
Re: Weird Join...Help Please

What does your current query look like.
Azkaban is offline   Fork this post Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the "right" join for the job? Azkaban Database Design 1 02-25-2005 02:36 PM
Cross dependency found in OUTER JOIN SQL rjmthezonenet SQL syntax 0 12-05-2004 04:41 PM
help with join shaunperry SQL syntax 0 10-07-2004 06:22 AM



All times are GMT -4. The time now is 06:48 AM.



Powered by: vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Google
  Web http://www.mysqltalk.com
DISCLAIMERS:
1. We have no commercial interest in this site.
Banner Ads and Subscriptions will only be used to help pay for hosting and maintenance costs.
2. MySQLTalk.com is NOT affiliated with MySQL AB in any way.
3. MySQLTalk.com is NOT endorsed by MySQL AB in any way.
4. Please do not post any content that is harmful to MySQL or MySQL AB, meaning no misleading or obsolete information will be tolerated.
Well-founded constructive criticism meant to help the community is permitted.
5. This website is founded with the goal of improving the MySQL community.
We not only tolerate newbies, we encourage them.
Please do not ask newbies to "read the manual".