0

ユーザーにとって最高の連勝を見つけるために、次のクエリを実行しています。

$sql="select sub.user_id as user_id,max(sub.streak) as streak,max(sub.units) as units
                from
                  (
                select 
                case when @x is null then @x:=user_id end,
                case 
                when awarded_unit>0 and @x=user_id then @y:=@y+1 
                when awarded_unit<0 and @x=user_id then @y:=0 
                when awarded_unit>0 and @x<>user_id then @y:=1
                when awarded_unit<0 and @x<>user_id then @y:=0
                end as streak,
                case 
                when awarded_unit>0 and @x=user_id then @z:=@z+awarded_unit 
                when awarded_unit<0 and @x=user_id then @z:=0 
                when awarded_unit>0 and @x<>user_id then @z:=awarded_unit 
                when awarded_unit<0 and @x<>user_id then @z:=0
                end as units,
                @x:=user_id as user_id,
                awarded_unit
                from $select_user_events_fights as u,$events as e,$event_fight_table as ef,$post_meta as pm where e.ID=ef.event_id
                and ef.event_fight_id=u.event_fight_id and  e.post_type='bt_events' and pm.post_id = e.ID and  e.post_status ='publish' and pm.meta_key='_event_dt_time' and u.`awarded_unit`!=0 and u.season_id=$season_id and u.user_id=".$user->ID."

                order by pm.meta_value desc,ef.fight_order desc
                  ) as sub
                group by sub.user_id";


            mysql_query("set @y=0;");
            mysql_query("set @x=null;");
            mysql_query("set @z=0;");

u.primary_key で注文するとうまくいきます。しかし、私はイベントを日付と戦いの順序でソートしたいと考えています。この場合、間違った結果になります。

order by ステートメントで内部クエリをチェックしました。

order by statement( "order by pm.meta_value desc,ef.fight_order desc") の場合、最良のストリークを計算した後に結果をソートしているため、間違った結果が得られます。

正しい答えを得る方法と、ここで何が欠けているかを親切に説明してください。ありがとう

4

1 に答える 1

1

サブクエリで ORDER BY を実行し、それを処理する外部クエリでストリーク検出を実行する必要があります。

これを試してください(テストされていません-テストしたい場合は、sqlfiddleでデータを提供してください):

      $sql="select sub.user_id as user_id,max(sub.streak) as streak,max(sub.units) as units
            from
              (
            select 
            case when @x is null then @x:=user_id end,
            case 
            when awarded_unit>0 and @x=user_id then @y:=@y+1 
            when awarded_unit<0 and @x=user_id then @y:=0 
            when awarded_unit>0 and @x<>user_id then @y:=1
            when awarded_unit<0 and @x<>user_id then @y:=0
            end as streak,
            case 
            when awarded_unit>0 and @x=user_id then @z:=@z+awarded_unit 
            when awarded_unit<0 and @x=user_id then @z:=0 
            when awarded_unit>0 and @x<>user_id then @z:=awarded_unit 
            when awarded_unit<0 and @x<>user_id then @z:=0
            end as units,
            @x:=user_id as user_id,
            awarded_unit
            from (select user_id, awarded_unit
                  from $select_user_events_fights as u,$events as e,$event_fight_table as ef,$post_meta as pm where e.ID=ef.event_id
            and ef.event_fight_id=u.event_fight_id and  e.post_type='bt_events' and pm.post_id = e.ID and  e.post_status ='publish' and pm.meta_key='_event_dt_time' and u.`awarded_unit`!=0 and u.season_id=$season_id and u.user_id=".$user->ID."

                  order by pm.meta_value desc,ef.fight_order desc) as subsub
            ) as sub
            group by sub.user_id";
于 2012-10-25T15:55:37.940 に答える