我们可以忽略COUNT个重复的行吗?
是的,我们可以使用DISTINCT忽略COUNT个重复的行。以下是语法:
select count(distinct yourColumnName) from yourTableName;
在MySQL中,COUNT()
将显示行数。DISTINCT用于忽略重复的行并仅获取唯一行的计数。
让我们首先创建一个表:
mysql> create table DemoTable(
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
FirstName varchar(10)
);
以下是使用insert命令在表中插入一些记录的查询:
mysql> insert into DemoTable(FirstName) values('Larry');mysql> insert into DemoTable(FirstName) values('John');
mysql> insert into DemoTable(FirstName) values('Sam');
mysql> insert into DemoTable(FirstName) values('John');
mysql> insert into DemoTable(FirstName) values('John');
mysql> insert into DemoTable(FirstName) values('Larry');
mysql> insert into DemoTable(FirstName) values('Mike');
mysql> insert into DemoTable(FirstName) values('Robert');
mysql> insert into DemoTable(FirstName) values('Carol');
mysql> insert into DemoTable(FirstName) values('Mike');
以下是使用select命令显示表中记录的查询:
mysql> select *from DemoTable;
这将产生以下输出:
+----+-----------+| Id | FirstName |
+----+-----------+
| 1 | Larry |
| 2 | John |
| 3 | Sam |
| 4 | John |
| 5 | John |
| 6 | Larry |
| 7 | Mike |
| 8 | Robert |
| 9 | Carol |
| 10 | Mike |
+----+-----------+
10 rows in set (0.00 sec)
以下是忽略COUNT个重复行并仅获取唯一行数的查询:
mysql> select count(distinct FirstName) from DemoTable;
这将产生以下输出:
+---------------------------+| count(distinct FirstName) |
+---------------------------+
| 6 |
+---------------------------+
1 row in set (0.03 sec)
以上是 我们可以忽略COUNT个重复的行吗? 的全部内容, 来源链接: utcz.com/z/316878.html