0

I Want To Join 2 Selections one:

SELECT user_email FROM users WHERE user_id = ?

And The Other Selection Is

SELECT f.friends_friend AS user_id, 
       users.username AS friend_name, 
       users.user_avtr AS profile_pic
FROM friends as f
LEFT OUTER JOIN(
    SELECT user_id,username,users.user_avtr FROM users
)AS users
ON f.friends_friend = users.user_id
WHERE f.friends_member = ?
UNION
SELECT f.friends_member AS user_id, 
       users.username AS friend_name, 
       users.user_avtr AS profile_pic
FROM friends as f
LEFT OUTER JOIN(
    SELECT user_id,username,users.user_avtr 
    FROM users
)AS users
ON f.friends_member = users.user_id
WHERE f.friends_friend = ?

i want to run those 2 queries in one query

2
  • Why could you not just add users.user_email to the SELECT statement in your second query since you have already made the join to the users table? Dec 31, 2011 at 17:55
  • Given the amount of information in your post, Jonathan's question seemed quite reasonable. His assumption was proved wrong by your comment to his answer, but then you should probably elaborate more on what is going on in your queries, whether they are supposed to receive the same value as their argument, and what the result should be like.
    – Andriy M
    Dec 31, 2011 at 23:08

1 Answer 1

0

Maybe, I misunderstand your question, but why couldn't you just do this?

SELECT f.friends_friend AS user_id, 
       users.username AS friend_name, users.user_email as user_email,
       users.user_avtr AS profile_pic
FROM friends as f
LEFT OUTER JOIN(
    SELECT user_email, user_id,username,users.user_avtr FROM users
)AS users
ON f.friends_friend = users.user_id
WHERE f.friends_member = ?
UNION
SELECT f.friends_member AS user_id, 
       users.username AS friend_name, users.user_email as user_email,
       users.user_avtr AS profile_pic
FROM friends as f
LEFT OUTER JOIN(
    SELECT user_email, user_id,username,users.user_avtr 
    FROM users
)AS users
ON f.friends_member = users.user_id
WHERE f.friends_friend = ?
3
  • I don't want the "friends" email. i want to pull my email from the DB, And I don't want to separate the queries because i want to PDO::fetchAll to one big array.
    – vintox
    Dec 31, 2011 at 18:28
  • Then just add the query you want to the union, add some dummy data, and make sure the aliases match. Jan 1, 2012 at 7:14
  • @vintox sorry, forgot to ping. Jan 3, 2012 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.