MySQL命令asc(如果优先级字段1移到顶部)

时间:2016-05-24 09:47:14

标签: php mysql

这是我的第一篇文章,我对MySql很新,所以如果这是一个简单的问题就道歉。我找到了几个答案,但可以让他们上班,所以我想发布并希望有人可以更清楚地解释一下。

所以我有一个名为“

”的玩家
  

player_id,player_name,player_morning_score,player_afternoon_score,player_priority

我进行查询以将早晨得分和下午得分一起添加,然后显示asc。这一切都有效。我需要做的是,如果2名玩家得分为21,我需要能够为其中一名玩家添加1的优先级,以便将他们移动到其他玩家之上。

继承我的代码。

SELECT player_id, player_name, (player_morning_score + player_afternoon_score) AS total
FROM players
ORDER BY total, ASC

我的结果显示如下

Craig 10
Ben   15
Jamie 15
Steve 7 

所以基本上我需要添加一个if stament来检查jamie或ben是否优先级为1,确定是否高于另一个。

可能有另一种方式来查询它,但是两个需要坐在顶部的玩家是下午得分最低的玩家。

希望这是足够的信息。提前谢谢。

3 个答案:

答案 0 :(得分:1)

如果我正确理解了您的要求,那么在关系情况下,您希望将下午得分最低的玩家放在最前面。

你可以试试这个:

SELECT player_id, player_name, 
      (player_morning_score + player_afternoon_score) AS total
FROM players
ORDER BY total ASC, player_afternoon_score DESC

答案 1 :(得分:0)

当两个或更多玩家得分相同时,你就会遇到这种情况。

那时您应该使用total检查player_priority,因为您的表中有优先级字段,并将player_afternoon_score视为DESC,以便获得最新播放器。

SELECT player_id, player_name, (player_morning_score + player_afternoon_score) AS total
FROM players
ORDER BY total,player_priority ASC, player_afternoon_score DESC

答案 2 :(得分:0)

你看到asc命令:

SELECT player_id, player_name, (player_morning_score + player_afternoon_score) AS total
FROM players
ORDER BY total

或desc order:

SELECT player_id,player_name,(player_morning_score + player_afternoon_score)AS总计

来自玩家

ORDER BY total desc