0

I have two tables: user and photo. They look like this:

Database tables

I need to perform a SELECT on the user table based on uuid, returning the url for both profile_photo and background_photo, if they exist.

These are essentially the final fields I need (the last two being JOINed from photo):

user.name, user.profile_photo_url, user.background_photo_url

WHERE user.uuid = SOME_UUID

Can somebody point me in the right direction with this statement?

1
  • Could you show us some data and what you need from it Commented Mar 15, 2016 at 5:39

2 Answers 2

1
SELECT user.name, photo_a.url AS profile_photo_url, photo_b.url as background_photo_url FROM user LEFT JOIN photo as photo_a ON user.profile_photo_uuid = photo_a.uuid LEFT JOIN photo as photo_b ON user.background_photo_uuid = photo_b.uuid WHERE user.uuid = SOME_ID
Sign up to request clarification or add additional context in comments.

4 Comments

This also works great, after altering background_photo_uuid to background_photo_url. Thanks!
Thanks. My mistake. Corrected my answer.
This is similar to Dylan Su's answer, but I'm curious which is "better" from a performance perspective. Any ideas?
Both answers are same except, for Dylan Su's answer, it will not return any user which has not either of the photo columns' entry in "photo" table. In my case, it will return each user with NULL values for photo URLs field, if their entries do not exist in "photo" table.
0

This should work for you

SELECT u.name,u.profile_photo_uuid,u.background_photo_url FROM user u, photo p 
WHERE u.uuid = **userid** AND 
( p.uuid = u.profile_photo_uuid  OR p.uuid = u.background_photo_url);

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.