排序依据:两列之间的差异

时间:2014-08-11 11:35:31

标签: php sql difference sql-order-by

我对sql查询有疑问。我在表格用户中有两列:

Moneytotal和Moneythisweek

现在我想根据差异订购结果。 Moneytotal和Moneythisweek之间的差异有多大,排名越高。

通常我会使用:

$lsel_rank = mysql_query("select * from users ORDER BY Moneytotal DESC");
$rank = mysql_fetch_array($lsel_rank);

但现在我想做点什么:

lsel_rank = mysql_query("select * from users ORDER BY Difference between Moneytotal         
AND Moneythisweek DESC");

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

SELECT * FROM `users` ORDER BY Moneytotal - Moneythisweek DESC

答案 1 :(得分:1)

只需使用此

即可
$lsel_rank = mysql_query("select *,'moneytotal - monethisweek' as difference from users ORDER BY difference DESC");

答案 2 :(得分:0)

选项1 :(按顺序进行计算)

SELECT * FROM `users` ORDER BY (Moneytotal - Moneythisweek) DESC;

选项2 :(按顺序使用编号索引并计算差异并在选择查询的结果集中使用

SELECT (Moneytotal - Moneythisweek) difference, a.*  FROM `users` a ORDER BY 1 DESC;