MySQL-按相似分类分组
我有这张桌子:MySQL-按相似分类分组,mysql,sql,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 u
/* 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;
但是结果并不令人满意,因为groupby
删除了重复的行
任何建议都很好。您想要的输出看起来更像是一个特定的排序,而不是一个分组。订购时请使用
订购人
:
SELECT * FROM users ORDER BY country;
你能再详细一点吗?您请求的是特定的
订单
,而不是分组人
。但是,为什么意大利排在最后?您打算按字母顺序排列,还是按值计数排列?