LEFT JOIN (select
case when @userId<>user_id then @q:=1 else @q:=@q+1 end q,
case when @q=1 then @userId:=user_id else @userId end user_id
from users_images, (select @userId:=0, @q:=1) t
where user_id<>@userId or (user_id=@userId and @q<=2)) ui ON ui.user_id = u.id