i have two tables. a user table and a photo table. IF a user uploads photo’s, they have the option of specifying only one as a ‘primary’ profile photo (enforced on the front-end).
tbl_users
---------
user_id
username
tbl_photos
---------
photo_id
user_id
is_primary (bit)
file_name
when i’m querying a list of users on a user search, for example, i want their chosen primary photo to display. however, if there are no linked records in the photo table or there are, but none are marked primary, I still want to return the users record and perhaps NULL or some other value for the file_name.
how would I accomplish this?
ps. i’m currently doing this through two separate calls to the database, but want this done in one call for efficiency.
Depending on what DB you use this works (
userid2querybeing the id of the user you want to retrieve):It returns the user information and the primary photo information – IF no primary photo or no photo at all it stills returns the user information but returns NULL for the photo information columns.