mySQL "get TOP 100 scores" 查询让我发疯

mySQL "get TOP 100 scores" query is turning me crazy

我已经问 this question 星期五,从那以后我一直在尝试我能想到的一切,但都没有成功

我无法让它工作

SELECT * FROM WorldFlowers_table GROUP BY device_id ORDER BY score DESC LIMIT 100 

和return我:

@1000111 和 Giorgos Betsos

一个人走进房间,说,"please help, I'm getting crazy trying with all my forces to solve a simple problem that any of you could solve in the blink of an eye"

2个人转身,朝那个人的脸开枪说"duplicate"

我累坏了:(

///////// 解决草莓评论

我尝试在 SQLFiddle 中构建它

CREATE TABLE WorldFlowers_table 
    (
     id int identity primary key, 
     timestamp varchar(20), 
     name varchar(30),
     score int,
     color varchar(30),
     flower varchar(30),
     device_id varchar(30),
    );

INSERT INTO WorldFlowers_table
(timestamp, name,score, color, flower, device_id  )
VALUES
('1475151826', 'RI-RI', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RO-RO', 46, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'ABC' ),
('1475151826', 'RI-RI', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RA-RA', 45, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'ABC' ),
('1475151826', 'RU-RU', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RE-RE', 44, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'DEF' ),
('1475151826', 'RY-RY', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RX-RX', 43, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'XYZ' ),
('1475151826', 'RA-RA', 42, '0|0|0|0|0|0|0|0|0|0', '[1475152768|42|4|0.0]', 'DEF' );

但是 2 次中只构建了 1 次。

我要找的return就是

+------+------------+--------------------------------------------+
| id   | timestamp  | name  | score | color | flower | device_id |                               
+------+------------+--------------------------------------------+
| 2    | blabla     | RO-RO |  46   |  ...  |   ...  |   ABC     | 
| 5    | blabla     | RE-RE |  44   |  ...  |   ...  |   DEF     |
| 7    | blabla     | RX-RX |  43   |  ...  |   ...  |   XYZ     |
+------+-------+-------------------------------------------------+

。每个 return

每个设备 ID 只有 1 个

。每个设备 ID 的最高分

。结果在 ORDER BY score DESC

中给出

更新:来自 scaisEdge 的这个似乎工作正常

SELECT * 来自分数 WHERE (姓名, 分数) IN ( SELECT 姓名, MAX(分数) FROM分数 按名称分组
按分数排序 ) 按分数排序 限制 100;

如果我没理解错的话,你想要前 100 名最高的设备得分

  SELECT *
  FROM WorldFlowers_table
  WHERE (device_id, score) IN (SELECT device_id, max(score) 
                               FROM WorldFlowers_table 
                               GROUP BY device_id )
  ORDER BY score DESC 
  LIMIT 100 

MySQL 不支持某些子查询运算符的子查询中的 LIMIT:

mysql> SELECT * FROM t1
    ->   WHERE s1 IN (SELECT s2 FROM t2 ORDER BY s1 LIMIT 1);
ERROR 1235 (42000): This version of MySQL doesn't yet support
 'LIMIT & IN/ALL/ANY/SOME subquery'

https://dev.mysql.com/doc/mysql-reslimits-excerpt/5.6/en/subquery-restrictions.html

看这里:

Problem with LIMIT & IN/ALL/ANY/SOME subquery

SELECT id, timestamp, name, score, color, flower, device_id
    FROM WorldFlowers_table 
    WHERE (device_id, score) IN 
    (SELECT device_id, MAX(score) FROM WorldFlowers_table GROUP BY device_id)
ORDER BY score DESC LIMIT 100;