展示具有重复记录的列中的不同记录数的 MySQL 查询


我们首先创建一个表 −

mysql> create table DemoTable
(
   StudentId int,
   StudentFirstName varchar(100)
);
Query OK, 0 rows affected (0.88 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable values(100,'John');
Query OK, 1 row affected (0.32 sec)
mysql> insert into DemoTable values(101,'Chris');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(102,'John');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values(103,'Sam');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(104,'Sam');
Query OK, 1 row affected (0.13 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

这将产生以下输出 −

+-----------+------------------+
| StudentId | StudentFirstName |
+-----------+------------------+
|       100 | John             |
|       101 | Chris            |
|       102 | John             |
|       103 | Sam              |
|       104 | Sam              |
+-----------+------------------+
5 rows in set (0.00 sec)

以下查询用于展示具有重复记录的列中的不同记录数 −

mysql> select count(distinct StudentFirstName) from DemoTable;

这将产生以下输出 −

+----------------------------------+
| count(distinct StudentFirstName) |
+----------------------------------+
|                                3 |
+----------------------------------+
1 row in set (0.00 sec)

更新日期: 2019-9 月 26 日

156 次浏览

开启您的 职业

通过完成课程获得认证

开始吧
广告