按 asc 和 desc 排序没有给出正确的结果

Order by asc and desc didnt give right result

我在获取查询结果时遇到问题。

id ,voteup , votedown

1  , 12     ,  7
2  ,  6     , 1 
3  ,  4     , 9 
4  ,  0     , 6 

我的查询是

 SELECT cm.id ,cm.voteup,cm.votedown  FROM Table1 cm  
 INNER JOIN ( select id ,MAX(voteup - votedown ) as maxe 
             from Table1 where voteup - votedown >= 5  group by id  ) tt
 on cm.id = tt.id
 ORDER BY cm.voteup DESC,cm.votedown asc,cm.id  limit 1

我得到这个结果

  id, voteup , votedown
   1, 12     ,  7

我想 select 最大结果 voteup - votedown >= 5 首先 如果两个结果相同,我想订购投票较少的那个

我希望有这个结果

 id , voteup , votedown
  2 ,  6     , 1         

谢谢。

这里是sqlfiddle Demo

我试过像那样更改列的顺序

  ORDER BY cm.votedown asc ,cm.voteup DESC limit 1

但是如果我有这样的值,这也会给我错误的结果

   id ,voteup , votedown

   1  , 6     ,  0
   2  , 9     , 2 
   3  ,  4     , 9 
   4  ,  0     , 6 

这给出

 1,  6 , 0

但是我想要

2 , 9 , 2    > which is max (9-2)

fiddle 最后一次尝试

您必须首先放置字段 maxe DESC,然后在 ORDER BY 子句中放置 cm.votedown asc 第一

SELECT cm.id ,cm.voteup,cm.votedown  FROM Table1 cm  
INNER JOIN ( 
   select id, MAX(voteup - votedown ) as maxe 
   from Table1 
   where voteup - votedown >= 5  
   group by id  
) tt on cm.id = tt.id
ORDER BY maxe DESC, cm.votedown asc, cm.voteup DESC, cm.id  limit 1

此查询将选择具有 最大 voteup - votedown 差异的记录。在平局的情况下,查询 returns 具有 最小 votedown 值的记录。

Demo here