1
SELECT *,`kills`-`deaths` as x FROM `users` ORDER BY `x` DESC LIMIT 0,50;

桌子:

user|kills|deaths
a   | 10  | 9
b   | 11  | 9
c   | 0   | 0
d   | 0   | 0
e   | 0   | 0
f   | 1   | 5

输出将是:

b | 2
a | 1
c | 0
d | 0
e | 0
f | -4

我需要这样的输出:

b | 2
a | 1
f | -4
c | 0
d | 0
e | 0

因为用户是活跃的并且他有杀戮和死亡,其他用户是不活跃的,所以他们走到了底部。

怎么做 ?

4

3 回答 3

2
SELECT *, kills-deaths as x 
FROM users 
ORDER BY x = 0, x DESC LIMIT 0,50;

SqlFiddle

于 2013-03-20T19:58:25.380 回答
0

只需将您的活动度量添加到该order by子句中即可。

SELECT *, (kills-deaths) as x 
FROM users 
ORDER BY ((kills+deaths) <> 0), x DESC 
LIMIT 50 OFFSET 0
于 2013-03-20T19:58:13.367 回答
0

我通常的快速解决方案是使用 group by

SELECT user,`kills`-`deaths` as x 
FROM `users` 
group by user 
ORDER BY `x` DESC 
LIMIT 0,50;

在这里你还可以

SELECT user,`kills`-`deaths` as x 
FROM `users` 
ORDER BY `kills`-`deaths` DESC 
LIMIT 0,50;

我使用 group by 使任何计算只发生在一个地方并且是一致的。

于 2013-03-20T20:08:02.013 回答