Jump to content

Left Join pass variable inside parentheses query???


GregL83

Recommended Posts

Hey all,

 

I'm trying to write a query that invovles 3 tables.  The tables are: users, user_pictures, and pictures.  Basically, each user can have multiple user_pictures, linked by user_id, which relate to a picture in the picture table with a unique picture_id.

 

users cols: id, name

user_pictures cols: user_id, picture_id

pictures cols: id, url

 

I need to return user_id and picture url.

 

here is the query I have thus far...  I was trying to use a variable inside the parenthese query to say only look for pictures that are relevant to the current user from the outer select statement...  Is there a way to pass that variable to the inner select statement??? or just a better way to write this query???

 

$query = "SELECT u.id AS uid, u.username, u.sex, u.birth_date, p.url AS picture, u.register_date FROM users u LEFT JOIN (SELECT us.user_id, us.picture_id, up.url FROM user_pictures us LEFT JOIN pictures up ON us.picture_id = up.id WHERE us.user_id = uid AND up.status = 'active' ORDER BY us.picture_id ASC LIMIT 1) p ON u.id = p.user_id ORDER BY u.id DESC LIMIT 5";

Link to comment
Share on other sites

Oh for multiple picture just change the where statement a little. Try this.

 

select users.id, users.username, users.sex, users.birth_date, pictures.url AS picture, users.register_date
from users, pictures, user_pictures
where users.id = user_pictures.user_id
and user_pictures.picture_id = pictures.id

Link to comment
Share on other sites

Ok so you want to reference a parent query field.. maybe something like this.

select users.id as uid, users.username, users.sex, users.birth_date, 
(select pictures.url 
from pictures, users_pictures 
where pictures.id = user_pictures.picture_id 
and user_pictures.user_id = uid 
and pictures.status = 'active' 
order by pictures.id desc limit 1
) as picture, users.register_date
from users

Link to comment
Share on other sites

Yes that would be a perf issue. Well another option to get the response back that he wants would be something like.

 

select users.id, users.username, users.sex, users.birth_date, pictures.url AS picture, users.register_date
from users, pictures,
(select user_pictures.user_id as uid, max(user_pictures.picture_id) pid
from user_pictures, pictures 
where pictures.id = user_pictures.picture_id
pictures.status = 'active'
group by user_pictures.user_id) t1
where t1.uid = users.id
and pictures.id = t1.pid

Link to comment
Share on other sites

SELECT 
  u.id AS uid, u.username, u.sex, u.birth_date, p.url AS picture, u.register_date 
FROM 
  users u 
LEFT JOIN (
  SELECT 
    us.user_id, us.picture_id, up.url 
  FROM 
    user_pictures us 
  LEFT JOIN 
    pictures up 
  ON 
    us.picture_id = up.id 
  WHERE 
    up.status = 'active' 
) p 
ON u.id = p.user_id 
ORDER BY u.id DESC LIMIT 5

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.