Jump to content

Recommended Posts

In my db I have the following tables/fields:-

 

users

user_id, name, surname

 

(This stores the user ids and their name and surname)

 

relationships

user_id, about_id, relationship

 

(This is a self join table attached to the users table showing relationships between users. A relationship of 2 means the users are friends)

 

photos

photo_id, user_id, photo_extension

 

(This contains all the photos. In this table user_id is used to determine what user uploaded the photo)

 

users_photos

user_id, photo_id, is_main

 

(This detemines what users are in/tagged in photos. The is_main field is used to determine if the photo has been set as a main profile picture for a user)

 

 

What I want now though is to be able to create a friends list for a logged in user where in each friend item the following is shown:-

 

- the profile picture of the friend i.e. is_main = 1 in the users_photos table

- each friends name and surname

- I also need the user id of each of the friends too which won't be shown but still needs to be retrieved

 

How do I achieve this using sql and joins?

 

So far I have the following, if we are to use a logged in user_id of 1 for example:-

 


SELECT users.user_id, users.name, users.surname, photos.photo_id, photos.photo_extension 
FROM users 
LEFT JOIN relationships 
ON (users.user_id=relationships.about_id) 
LEFT JOIN users_photos 
ON ( users_photos.user_id = relationships.about_id ) 
LEFT JOIN photos 
ON ( photos.photo_id = users_photos.photo_id ) 
WHERE relationships.user_id = 1 AND relationship = 2 


 

However using the above returns the photo_id and photo_extension as NULL

 

Can anyone please help me out in fixing this.

 

Link to comment
https://forums.phpfreaks.com/topic/233959-sql-joins-to-get-users-friends-photos/
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.