MySQL 查询如何计算不同值的出现次数,并将结果显示在新列中?
我们首先创建一个表 −
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.57 sec)
使用插入命令在表中插入一些记录 −
mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.12 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-------+ | Value | +-------+ | 80 | | 90 | | 90 | | 80 | | 100| | 90 | +-------+ 6 rows in set (0.00 sec)
以下是用于计算不同值出现次数的查询 −
mysql> select Value,count(*) from DemoTable group by Value;
这将产生以下输出 −
+-------+----------+ | Value | count(*) | +-------+----------+ | 80 | 2 | | 90 | 3 | | 100 | 1 | +-------+----------+ 3 rows in set (0.00 sec)
广告