如何计算MySQL数据库中一列的唯一记录?
为此,使用聚合函数count(*)计数到GROUP BY进行分组。让我们首先创建一个表-
mysql> create table DemoTable(
UserName varchar(100),
UserPostMessage text
);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris','Hi');mysql> insert into DemoTable values('David','Hello');
mysql> insert into DemoTable values('Chris','Awesome');
mysql> insert into DemoTable values('Chris','Amazing');
mysql> insert into DemoTable values('David','Nice Place');
mysql> insert into DemoTable values('Chris','Amazing');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----------+-----------------+| UserName | UserPostMessage |
+----------+-----------------+
| Chris | Hi |
| David | Hello |
| Chris | Awesome |
| Chris | Amazing |
| David | Nice Place |
| Chris | Amazing |
+----------+-----------------+
6 rows in set (0.00 sec)
这是查询以对我的MySQL数据库中的列进行唯一记录计数的查询-
mysql> select UserName,count(DISTINCT UserPostMessage) AS NumberOfMessagesPostByUser from DemoTable group by UserName;
这将产生以下输出-
+----------+----------------------------+| UserName | NumberOfMessagesPostByUser |
+----------+----------------------------+
| Chris | 3 |
| David | 2 |
+----------+----------------------------+
2 rows in set (0.00 sec)
以上是 如何计算MySQL数据库中一列的唯一记录? 的全部内容, 来源链接: utcz.com/z/350340.html