问题
I have asked this question Friday and since then I have been trying everything I could think of with no success
I cannot get this to work
SELECT * FROM WorldFlowers_table GROUP BY device_id ORDER BY score DESC LIMIT 100
and return me :
the 100 rows with the top 100 scores
filtered so that only the highest score of each device_id is featured
ORDER BY score DESC
@ 1000111 and Giorgos Betsos
A guy comes into a room, says, "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 guys turn around, shoot the guy in the face and say "duplicate"
I am getting exhausted :(
///////// to adress Strawberry comment
I tried building that in 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' );
but is does only builds one time out of 2.
The return I am looking for would then be
+------+------------+--------------------------------------------+
| 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 |
+------+-------+-------------------------------------------------+
. only 1 of each device-id per return
. the highest score per device-id
. and the result being given in ORDER BY score DESC
UPDATE: this from scaisEdge seems to work fine
SELECT * FROM scores
WHERE (name, score) IN ( SELECT name, MAX(score)
FROM scores
GROUP BY name
ORDER BY score DESC
)
ORDER BY score DESC
LIMIT 100 ;
回答1:
If i undestand right you want the top 100 highest device score
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
回答2:
MySQL does not support LIMIT in subqueries for certain subquery operators:
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
Take a look here:
Problem with LIMIT & IN/ALL/ANY/SOME subquery
回答3:
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;
来源:https://stackoverflow.com/questions/39833606/mysql-get-top-100-scores-query-is-turning-me-crazy