請檢查下面的例子中它會幫助你對這個
select * from games order by w;
+--------+------+------+------+------+
| name | p | w | d | l |
+--------+------+------+------+------+
| team b | 1 | 0 | 1 | 0 |
| team a | 1 | 0 | 0 | 1 |
| team c | 1 | 1 | 0 | 0 |
| team d | 2 | 2 | 0 | 0 |
+--------+------+------+------+------+
4 rows in set (0.00 sec)
mysql> SELECT NAME,P,W,D,L,@curRank := @curRank + 1 as rank FROM (select NAME,P,W,D,L from games order by w DESC) x,(SELECT @curRank := 0) r;
+--------+------+------+------+------+------+
| NAME | P | W | D | L | rank |
+--------+------+------+------+------+------+
| team d | 2 | 2 | 0 | 0 | 1 |
| team c | 1 | 1 | 0 | 0 | 2 |
| team b | 1 | 0 | 1 | 0 | 3 |
| team a | 1 | 0 | 0 | 1 | 4 |
+--------+------+------+------+------+------+
4 rows in set (0.01 sec)
mysql> SELECT NAME,P,W,D,L,rank FROM (SELECT NAME,P,W,D,L,@curRank := @curRank + 1 as rank,if(name ="team b",@curRank := @curRank*-1,0) AS required_rank FROM (select NAME,P,W,D,L from games order by w DESC) x,(SELECT @curRank := 0) r) p order by abs(rank) desc limit 3;
+--------+------+------+------+------+------+
| NAME | P | W | D | L | rank |
+--------+------+------+------+------+------+
| team b | 1 | 0 | 1 | 0 | 3 |
| team c | 1 | 1 | 0 | 0 | 2 |
| team a | 1 | 0 | 0 | 1 | -2 |
+--------+------+------+------+------+------+
3 rows in set (0.00 sec)
好了,一旦情況下,你必須用''地方asc'和其他'點asc'秩序。我期望'點desc'和'點asc'。 –
您好Gordon,感謝您的快速反應,asc/desc問題只是我嘗試使其工作時犯的一個小錯誤。但它仍然不起作用。 –