3

I'd like to loop through the results of a select subquery and use them yo fetch data off of another table:

SELECT alias, profile_picture 
FROM public.profile 
where user_id = (
    SELECT friend_user_id 
    FROM public.friends 
    where user_id = 1059948007363671)::text

obviously this query returns:

ERROR:  more than one row returned by a subquery used as an expression

What would be the best way to loop through the subquery results and assign it to a temporary variable that pubic.profile could use to fetch the respective profile info?

2
  • SELECT alias, profile_picture FROM public.profile JOIN public.friends on profile.user_id=friends.friend_user_id WHERE friends.user_id = 1059948007363671 Commented Dec 28, 2015 at 22:02
  • yea that did the trick =) thanks. I cant vote for your answer since its a comment. maybe you wanna re-post it as a questiion?? Commented Dec 29, 2015 at 3:22

2 Answers 2

2

Use in instead of =:

WHERE user_id IN 
    (
    SELECT friend_user_id::text
    FROM public.friends 
    WHERE user_id = 1059948007363671
    )
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT alias, profile_picture 
FROM public.profile 
JOIN public.friends 
ON profile.user_id=friends.friend_user_id 
WHERE friends.user_id = 1059948007363671

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.