简体   繁体   English

选择与表mysql不同的

[英]select distinct from table mysql

I cant filter in a good way my table, this is my table: 我不能很好地过滤我的桌子,这是我的桌子:

+----------+-----------+-------+------+
| 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

I want to order it by Bigger Level and Time, but not repeating FirstName and only 5 first, its posible? 我想按更大的级别和时间订购它,但不重复名字,只重复5个,这可能吗? thanks. 谢谢。

So my Output Should be some stuff like this: 所以我的输出应该是这样的:

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

I hope someone can help me. 我希望有一个人可以帮助我。 I know what to use, but i dont know how to use it. 我知道该怎么用,但我不知道该怎么用。

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

thanks everyone, obviusly that mysql script doesnt work, i hope you know how to do it, :) 谢谢大家,很明显,mysql脚本无法正常工作,我希望您知道该怎么做,:)

You can use variables in order to get the greatest-per-player record: 您可以使用变量以获得最高的每人记录:

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 在这里演示

This can be done with a fiddle using GROUP_CONCAT:- 这可以通过使用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

To order that in the right order:- 以正确的顺序订购:-

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 fiddle for it here:- SQL在这里摆弄:-

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

Note that this ordering won't be that efficient (no way to use indexes). 请注意,这种排序方式效率不高(无法使用索引)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM