0

I am trying to use the same value from a SELECT statement multiple times.
The beginning of my query looks like this:

IF EXISTS (SELECT * FROM `friends` WHERE (`Friend1ID`=value OR `Friend2ID`=value))...

value should be the result of a SELECT query, say:

SELECT `ID` FROM `users` WHERE `name`='daniel'

I could use the same SELECT statement twice, but are there any other possibilities that would make the query "cleaner"?

1 Answer 1

2

If you are simply trying to get a list of friends with an associated user name of 'daniel' then you can use an INNER JOIN.

SELECT 
    * 
FROM  
    friends
    INNER JOIN users ON users.ID=friends.friends1ID OR users.ID=friends.friends2ID

WHERE
    users.name='daniel'
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.