Baraa
Baraa

Reputation: 697

Select query from mysql database

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!

Upvotes: 0

Views: 125

Answers (2)

Max
Max

Reputation: 165

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

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

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)

Upvotes: 1

Related Questions