我可以使用MySQL获取列中重复值的计数吗?
是的,您可以将ORDER BY DESC与GROUP BY一起使用。让我们首先创建一个表-
create table DemoTable-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> PostMessage varchar(100)
-> );
使用插入命令在表中插入一些记录-
insert into DemoTable(PostMessage) values('Hi');insert into DemoTable(PostMessage) values('Hello');
insert into DemoTable(PostMessage) values('Hi');
insert into DemoTable(PostMessage) values('Awesome');
insert into DemoTable(PostMessage) values('Hello');
insert into DemoTable(PostMessage) values('Hi');
insert into DemoTable(PostMessage) values('Awesome');
使用select语句显示表中的所有记录-
select *from DemoTable;
输出结果
这将产生以下输出-
+----+-------------+| Id | PostMessage |
+----+-------------+
| 1 | Hi |
| 2 | Hello |
| 3 | Hi |
| 4 | Awesome |
| 5 | Hello |
| 6 | Hi |
| 7 | Awesome |
+----+-------------+
7 rows in set (0.00 sec)
以下是查询以获取MySQL数据库中最大的值-
select PostMessage,count(Id) from DemoTable group by PostMessage-> order by count(Id) DESC;
输出结果
这将产生以下输出-
+-------------+-----------+| PostMessage | count(Id) |
+-------------+-----------+
| Hi | 3 |
| Hello | 2 |
| Awesome | 2 |
+-------------+-----------+
3 rows in set (0.00 sec)
如果只想要最高,则可以使用以下查询-
select PostMessage,count(Id) from DemoTable group by PostMessage having count(Id) > 2;
输出结果
这将产生以下输出-
+-------------+-----------+| PostMessage | count(Id) |
+-------------+-----------+
| Hi | 3 |
+-------------+-----------+
1 row in set (0.00 sec)
以上是 我可以使用MySQL获取列中重复值的计数吗? 的全部内容, 来源链接: utcz.com/z/327321.html