TABLE users_community_relations
list_id int user_id int dest_user_id int
1 10 20
2 10 24
3 10 33
4 11 10
TABLE users_avatar
avatar_id int user_id int webavatar_thumbnail
1 10 test.jpg
2 10 test2.jpg
3 20 test2.jpg
4 20 test11.jpg
TABLE users
user_id int nick varchar
10 kaka
20 caca
24 dada
33 roro
I need to query this three tables there is the logic, table users_community_relations wheen i put user_id = 10 for example i get results from same table column dest_user_id 20,24,33 each number represent user_id in next two tables, i would need to get all data from next two tables accordingly to 20,24,33 numbers from dest_user_id column. Sometimes table users_avatar does not hold any avatar for user in that case string from this table could be empty. Sometimes it holds more than two rows, in that case i would need to get only first one but ordered with avatar_id DESC users_avatar table.
This should work:
It uses joins to ensure we only return the user_avatar row with the highest avatar_id or NULL if there is no avatar.