我认为你可以做这样的事情 -
SELECT rank, GROUP_CONCAT(name) FROM (
-- your query goes here
...
...
...
) t
GROUP BY rank
CREATE TABLE marks(
name VARCHAR(20),
z INT(10),
y INT(10),
total_marks INT(10),
rank INT(10)
);
INSERT INTO marks (name, z, y, total_marks, rank) VALUES ('abc', 100, 50, 150, 1);
INSERT INTO marks (name, z, y, total_marks, rank) VALUES ('abc', 100, 50, 150, 2);
INSERT INTO marks (name, z, y, total_marks, rank) VALUES ('abc', 100, 50, 150, 3);
INSERT INTO marks (name, z, y, total_marks, rank) VALUES ('def', 100, 50, 150, 1);
INSERT INTO marks (name, z, y, total_marks, rank) VALUES ('def', 100, 50, 150, 2);
INSERT INTO marks (name, z, y, total_marks, rank) VALUES ('abcdef', 100, 50, 150, 1);
INSERT INTO marks (name, z, y, total_marks, rank) VALUES ('abcdef', 100, 50, 150, 2);
INSERT INTO marks (name, z, y, total_marks, rank) VALUES ('abcdef', 100, 50, 150, 3);
SELECT rank, GROUP_CONCAT(name) FROM (
SELECT name, MAX(rank) rank FROM marks GROUP BY name) t
GROUP BY rank;
+------+--------------------+
| rank | GROUP_CONCAT(name) |
+------+--------------------+
| 2 | def |
| 3 | abc,abcdef |
+------+--------------------+
用户abc和abcdef具有相同的等级 (1,2,3)。用户def有不同的等级 (1,2)。