展示具有重複記錄的列中的不同記錄數的 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)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP