mysql - 在 mysql 中选择带有 order by 的案例

标签 mysql

我正在运行以下查询来查找用户的最佳连胜记录。

$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 语句("order by pm.meta_value desc,ef.fight_order desc") ,它是对结果进行排序 在计算最佳连胜后,它给出了错误的结果。

请解释如何获得正确答案以及我在这里缺少的内容。谢谢

最佳答案

您需要在子查询中执行 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";

关于mysql - 在 mysql 中选择带有 order by 的案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072230/

相关文章:

php - 在MAMP中导入文件(文件大小超出允许的最大大小)

mysql - 引用外键连接两个表

mysql - 在忽略年份的日期范围之间选择

mysql - 如何使用 ActiveRecord 将 DISTINCT ON 与 mysql 一起使用

java - Hibernate不保存时间戳字段

mysql - 如何在 CakePHP 中使用聚合 Mysql 函数

mysql - 为什么 R 上传数据比 KNIME 或 Workbench 快得多?

mysql - 如何使用其他三个表中列的平均值更新一个表中的分数列?

Mysql子查询问题

java - 用于数据库规范化的准确表格