sql - Incorrect data when run as subquery -
i have query takes data given media table joins user table :
select media.id, media.user_id,@rownum := @rownum + 1 position inner join users on media.user_id = users.id media_table order media.distance asc, media.media_likes_count desc, media.media_views_count desc;
this query produces nice looking table follows:
media_id, user_id, position 39199 , 3949 , 1 39299 , 3149 , 2 39359 , 3944 , 3 39369 , 3349 , 4 39379 , 3149 , 5 39389 , 3449 , 6
from derived table, want position of media_id = 39389.
however if include query in subquery this:
select position (select media.id, media.user_id,@rownum := @rownum + 1 position inner join users on media.user_id = users.id media_table order media.distance asc, media.media_likes_count desc, media.media_views_count desc;) media_id = 39389
then columns 'shuffle' , 39389 not have position 6 anymore.
not withstanding errors in query, assuming typos, perhaps you're issue not initializing user defined variable
. condensed version works me:
select postition ( select yourresults.*, @rn:=@rn+1 postition yourresults join (select @rn:= 0) t order media_id ) t media_id = 39389
while not work:
select postition ( select yourresults.*, @rn:=@rn+1 postition yourresults order media_id ) t media_id = 39389
Comments
Post a Comment