Mysql
排序表並獲取行的位置
我有一個有 3 行的表格。播放器、時間、伺服器。
現在,我想要兩件事,但我不知道怎麼做。
對於伺服器,有 3 個值可用。1、2和3。伺服器和播放器一起是主鍵。使用者是 VARCHAR,伺服器和時間是字元串。我使用 1,2 和 3 讓我更容易理解。
第一個我想按時間對錶格進行排序並獲取特定玩家的位置,但只能來自 1 個伺服器。
例子:
User Time Server 2 10 1 1 50 1 2 60 3 1 20 2 1 100 3 2 70 2
現在,想知道使用者 2 的位置
如果我按時間和伺服器排序 = 1
- 使用者 2 將是 2 號
- 使用者 1 號碼 1
如果伺服器 = 2
- 使用者 2 將是 1
- 使用者 1 將是 2
然後我想得到使用者位置,如果我按使用者時間的總和排序。在這種情況下,使用者 1 將是 1(時間總和:170),使用者 2 將是 2(時間總和:100)。
我希望你可以幫助我!順便說一句,我想在 Java 中使用它;)
~java4ever/羅賓
查詢 #1
如果我按時間排序並且伺服器 = 1,則使用者 2 的位置
SET @given_user = 2; SET @given_server = 1; SET @pos = 0; SELECT * FROM ( SELECT (@pos:=@pos+1) pos,User FROM playerstats WHERE server = @given_server ORDER BY time DESC ) A WHERE User = @given_user;
查詢 #2
如果我按使用者的時間總和排序
SET @pos = 0; SELECT (@pos:=@pos+1) pos,User,TotalUserTime FROM ( SELECT User,SUM(time) TotalUserTime FROM playerstats GROUP BY User ) A ORDER BY TotalUserTime DESC;
您的樣本數據
mysql> DROP DATABASE IF EXISTS mydb; Query OK, 0 rows affected, 1 warning (0.03 sec) mysql> CREATE DATABASE mydb; Query OK, 1 row affected (0.01 sec) mysql> USE mydb Database changed mysql> CREATE TABLE playerstats -> ( -> User INT, -> Time INT, -> Server INT -> ) ENGINE=MyISAM; Query OK, 0 rows affected (0.25 sec) mysql> INSERT INTO playerstats VALUES -> (2, 10,1), -> (1, 50,1), -> (2, 60,3), -> (1, 20,2), -> (1,100,3), -> (2, 70,2); Query OK, 6 rows affected (0.03 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM playerstats; +------+------+--------+ | User | Time | Server | +------+------+--------+ | 2 | 10 | 1 | | 1 | 50 | 1 | | 2 | 60 | 3 | | 1 | 20 | 2 | | 1 | 100 | 3 | | 2 | 70 | 2 | +------+------+--------+ 6 rows in set (0.01 sec) mysql>
查詢 #1 已執行
mysql> SET @given_user = 2; Query OK, 0 rows affected (0.00 sec) mysql> SET @given_server = 1; Query OK, 0 rows affected (0.00 sec) mysql> SET @pos = 0; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM -> ( -> SELECT (@pos:=@pos+1) pos,User -> FROM playerstats -> WHERE server = @given_server -> ORDER BY time DESC -> ) A -> WHERE User = @given_user; +------+------+ | pos | User | +------+------+ | 2 | 2 | +------+------+ 1 row in set (0.09 sec) mysql>
查詢 #2 已執行
mysql> SET @pos = 0; Query OK, 0 rows affected (0.00 sec) mysql> SELECT (@pos:=@pos+1) pos,User,TotalUserTime FROM -> ( -> SELECT User,SUM(time) TotalUserTime -> FROM playerstats -> GROUP BY User -> ) A ORDER BY TotalUserTime DESC; +------+------+---------------+ | pos | User | TotalUserTime | +------+------+---------------+ | 1 | 1 | 170 | | 2 | 2 | 140 | +------+------+---------------+ 2 rows in set (0.00 sec) mysql>
試一試 !!!
更新 2014-12-26 15:32 EST
查詢 #3
SET @given_user = 2; SET @pos = 0; SELECT * FROM ( SELECT (@pos:=@pos+1) pos,User,TotalUserTime FROM ( SELECT User,SUM(time) TotalUserTime FROM playerstats GROUP BY User ) A ORDER BY TotalUserTime DESC ) AA WHERE User = @given_user;
查詢 #3 已執行
mysql> SET @given_user = 2; Query OK, 0 rows affected (0.00 sec) mysql> SET @pos = 0; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM -> ( -> SELECT (@pos:=@pos+1) pos,User,TotalUserTime FROM -> ( -> SELECT User,SUM(time) TotalUserTime -> FROM playerstats -> GROUP BY User -> ) A ORDER BY TotalUserTime DESC -> ) AA -> WHERE User = @given_user; +------+------+---------------+ | pos | User | TotalUserTime | +------+------+---------------+ | 2 | 2 | 140 | +------+------+---------------+ 1 row in set (0.06 sec) mysql>