0

I have a relationship table in my database that has four values AKA...

id, int, primary, auto-incriment 
mem1, int
mem2, int
type, enum('a','b')

I also have another table that has the members id, username, firstname, and lastname

I want to click a button and get the relationships between the two people without duplicates. Here is the query I have so far...

SELECT 
    myMembers.id AS id, 
    myMembers.username AS username, 
    myMembers.firstname AS firstname,
    myMembers.lastname AS lastname 
FROM myMembers 
INNER JOIN memberRelations 
    ON memberRelations.mem2=myMembers.id
WHERE memberRelations.mem1='$id' OR memberRelations.mem2='$id' ORDER BY myMembers.username 
ASC

The problem here is that on some users it gives me the correct values, but on others it gives me the profile user and not the other member relationship. I suspect this has to do with the ON portion of the query, but truthfully I am lost! Is there a better way to accomplish my goal? Or can I just correct something in my query? Any help would be greatly appreciated. Thanks!

1 Answer 1

1

Based on your question, I think you want the following which selects both members of the relationship:

SELECT
    R.type,
    M1.id AS id1, 
    M1.username AS username1, 
    M1.firstname AS firstname1,
    M1.lastname AS lastname1,
    M2.id AS id1, 
    M2.username AS username2, 
    M2.firstname AS firstname2,
    M2.lastname AS lastname2
FROM memberRelations AS R
       INNER JOIN myMembers AS M1 ON R.mem1=M1.id
       INNER JOIN myMembers AS M2 ON R.mem2=M2.id
WHERE R.mem1='$id' OR R.mem2='$id'
Sign up to request clarification or add additional context in comments.

Comments

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.