选择不同于表mysql

时间:2016-01-26 10:52:57

标签: mysql

我无法过滤我的桌子,这是我的桌子:

+----------+-----------+-------+------+
| PersonID | FirstName | Level | Time |
+----------+-----------+-------+------+
|    1     |   Beru    |   51  |  22  |
|    2     |   Pepe    |   42  |  13  |
|    3     |   Tom     |   30  |  15  |
|    4     |   Beru    |   51  |  32  |
|    5     |   Tom     |   28  |  15  |
|    ....  |    ...    |  .... |  ... |
+----------+-----------+-------+------+
    ........................... more players

我想通过更大的等级和时间来订购它,但不是重复FirstName而只重复5,它的可能性?感谢。

所以我的输出应该是这样的东西:

- FirstName - Level - Time
- Beru      - 51    - 32
- Pepe      - 42    - 13
- Tom       - 30    - 15
-  .... more players...

我希望有人可以帮助我。我知道该用什么,但我不知道如何使用它。

SELECT o.FirstName, o.Level, o.Time
FROM myTable o
GROUP BY FirstName
ORDER BY o.Level*100+o.Time  DESC
LIMIT 0,10

感谢大家,很明显mysql脚本不起作用,我希望你知道怎么做,:))

2 个答案:

答案 0 :(得分:1)

您可以使用变量来获得最佳的每位玩家记录:

SELECT PersonID, FirstName, Level, Time
FROM (
  SELECT PersonID, FirstName, Level, Time,
         @rn := IF(@name = FirstName, @rn + 1,
                   IF(@name := FirstName, 1, 1)) AS rn                 
  FROM mytable
  CROSS JOIN (SELECT @rn := 0, @name := '') AS vars
  ORDER BY FirstName, Level DESC, Time DESC) AS t
WHERE rn = 1
ORDER BY FirstName LIMIT 0, 5

Demo here

答案 1 :(得分:1)

这可以通过使用GROUP_CONCAT的小提琴来完成: -

SELECT FirstName, 
        SUBSTRING_INDEX(GROUP_CONCAT(Level ORDER BY Level DESC, Time DESC), ',', 1) AS Level,
        SUBSTRING_INDEX(GROUP_CONCAT(Time ORDER BY Level DESC, Time DESC), ',', 1) AS Time
FROM myTable
GROUP BY FirstName

按正确顺序订购: -

SELECT FirstName, 
        SUBSTRING_INDEX(GROUP_CONCAT(Level ORDER BY Level DESC, Time DESC), ',', 1) AS Level,
        SUBSTRING_INDEX(GROUP_CONCAT(`Time` ORDER BY Level DESC, Time DESC), ',', 1) AS `Time`
FROM myTable
GROUP BY FirstName
ORDER BY Level*100+`Time`  DESC
LIMIT 10

SQL在这里小提琴: -

http://www.sqlfiddle.com/#!9/03457/1

请注意,此排序效率不高(无法使用索引)。