0

Hello I am having a hard time trying to SELECT all of the information I need from two tables, the following two tables are:

Person: |id|fname|mname|lname|   and Related: |id1|id2|relationship|

and I want the following to be displayed back from the SELECT query:

|id1|fname(of id1)|id2|fname(of id2)|relationship|

SO the Related table has two id's that are FOREIGN KEYS to Person(id), and I need to SELECT id1, (id1's first name), id2, (id2's firstname), and the relationship.

I've tried something like this and a few other SELECT queries but I can't seem to get it to work:

SELECT p.fname, r.id1, r.id2, r.relationship
FROM Person p, Related r
INNER JOIN Related ON first.id = r.id1 
INNER JOIN Related ON second.id = r.id2;

Any help will be greatly appreciated! Thank you!

2 Answers 2

1

You're joining on Related thrice. You only need to join once, and you need to join on Person again.

SELECT id1, p1.fname, id2, p2.fname, relationship
FROM Person p1
JOIN Related ON (p1.id = id1)
JOIN Person p2 ON (id2 = p2.id)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much this worked it will be the accepted answer as soon as I can accept!
0

I've found a website for you (w3schools) and it should have all the information you need for the SELECT function you're trying to get. Hope this helps: http://www.w3schools.com/php/php_mysql_select.asp

1 Comment

Yeah I actually know w3schools and I have searched through things and I've done other queries that worked based upon things I learned from there...but this query I just couldn't get to work but thanks for the info!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.