如何在MySQL中计算不同的值?
要计算非重复值,可以在聚合函数中使用非重复值count()。
语法如下-
select count(distinct yourColumnName) as anyVariableName from yourTableName;
为了理解上述概念,让我们创建一个表。以下是创建表的查询-
mysql> create table DistinctDemo −> ( −> Name varchar(200) −> );
对于我们的示例,让我们在表中插入重复的记录。插入记录的查询如下-
mysql> insert into DistinctDemo values('John');
mysql> insert into DistinctDemo values('Sam');
mysql> insert into DistinctDemo values('John');
mysql> insert into DistinctDemo values('Johnson');
mysql> insert into DistinctDemo values('John');
mysql> insert into DistinctDemo values('Johnson');
mysql> insert into DistinctDemo values('Sam');
mysql> insert into DistinctDemo values('Johnson');在select语句的帮助下显示所有记录。显示所有记录的查询如下-
mysql> select *from DistinctDemo;
以下是显示一些重复记录的输出-
+---------+ | Name | +---------+ | John | | Sam | | John | | Johnson | | John | | Johnson | | Sam | | Johnson | +---------+ 8 rows in set (0.00 sec)
这是可用于计算表中不同值的查询-
mysql> select count(distinct Name) as DistinctValues from DistinctDemo;
以下是输出-
+----------------+ | DistinctValues | +----------------+ | 3 | +----------------+ 1 row in set (0.01 sec)
结果i3告诉我们表中有3个不同的值。
热门推荐
10 职场寄语老师祝福语简短
11 爱情红包祝福语简短英文
12 鸡年祝福语简短8字
13 妹妹搬家简短祝福语大全
14 学生励志祝福语大全简短
15 生日毕业祝福语简短英文
16 医院探望朋友祝福语简短
17 祝福语简短唯美老师的话
18 女儿发型生日祝福语简短