Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:13:40+00:00 2026-06-13T10:13:40+00:00

I am running following query to find the best winning streak for user. $sql=select

  • 0

I am running following query to find the best winning streak for user.

$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;");

It works fine if I put order by u.primary_key. But I wants events to be sorted by date and fight order . it gives wrong result in this case.

I had checked inner query with order by statement.

for order by statement("order by pm.meta_value desc,ef.fight_order desc") ,it is sorting results
after calculating the best streak so it is giving wrong result.

Kindly explain how to get correct answer and what I am missing here.Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:13:42+00:00Added an answer on June 13, 2026 at 10:13 am

    You need to do the ORDER BY in a subquery, and then perform the streak detection in an outer query that processes that.

    Try this (untested — if you want me to test, supply some data in a 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";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running following query. SELECT T1.C1, T2.C2..., IF( T1.C1<>T2.C1,Changed,1) AS NewColumn From T1
I am running following query.. Select T1.* from T1 LEFT JOIN T2 ON T1.C1
I'm currently running the following query: SELECT * from people WHERE id = 4;
Using phpMyAdmin and running the following query I get 19 registers: SELECT * FROM
When running the following the query. select * from surgicals where to_char(dt_surgery ,'DD-MM-YYYY' )
I am running following query in SQL*Plus CREATE TABLE tbl_audit_trail ( id NUMBER(11) NOT
I tried running the following query to restore a database: restore database myDatabase from
I'm running the following query SELECT ROW_NUMBER() OVER(ORDER BY [TransactionValues].[ID]) AS idx, [Transactions].[ID] AS
I have the following query: SELECT wm_concat(DISTINCT NAME) as Methods FROM TPM_TRAININGPLAN JOIN TPM_DELIVERYMETHODS
I have the following SQL Query I'm executing and I'm trying to find why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.