MySQL - 按类别分组

时间:2014-03-05 15:03:41

标签: mysql sql

我有这张桌子:

/* Create a table called users */
CREATE TABLE users(Id integer PRIMARY KEY, Name text, country text);

/* Create few records in this table */
INSERT INTO users VALUES(1,'Tom','Canada');
INSERT INTO users VALUES(2,'Lucy','USA');
INSERT INTO users VALUES(3,'Frank','USA');
INSERT INTO users VALUES(4,'Jane','Canada');
INSERT INTO users VALUES(5,'Robert','Italy');
COMMIT;

我想要一个取代此结果的查询:

+---+--------+--------+
| 1 |  Tom   | Canada |
+---+--------+--------+
| 4 | Jane   | Canada |
| 3 | Frank  | USA    |
| 2 | Lucy   | USA    |
| 5 | Robert | Italy  |
+---+--------+--------+

表示各国的分类用户。为此,我使用此查询:

SELECT * FROM users group by country;

但结果并不令人满意,因为group by删除了重复的行。

任何建议都会很好。

1 个答案:

答案 0 :(得分:0)

您想要的输出看起来更像是特定的排序而不是分组。如需订购,请使用ORDER BY

SELECT * FROM users ORDER BY country;