If I have these two rows:
Users Table UserId | Username | Etc...
5 | John |
Avatars Table Id | UserId | ImageName | ImageType
1 | 5 | example.png | original
2 | 5 | example_thumb.png | thumbnail
And I have:
SELECT * FROM Users JOIN Avatars
ON Users.UserId=Avatars.UserId WHERE Users.UserId = '5'
Do I have to add the additional:
AND Avatars.ImageType = 'thumbnail'
Or is there a way to select all and get the one I want with php (much like an array: $var[‘something’])??
Or is it possible to distinguish them with mysql using something like the ‘AS’ clause??
Without further qualifying the join with the
ImageTypecondition, you will get multiple rows per user record.Edit: For example
Edit #2: If you want both images in one row, use this query
Note that this will only return rows where both image types are present for the user in
Avatars. If you want to get a result irrespective of the existence of avatar images, use left joins.