在 MySQL 中获取不同类型的用户数量?
我们先创建一个表 −
mysql> create table DemoTable -> ( -> UserName varchar(20), -> UserType ENUM('New User','Registered User') -> ); Query OK, 0 rows affected (0.60 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('Chris','New User'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David','New User'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Mike','Registered User'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Sam','New User'); Query OK, 1 row affected (0.10 sec)
使用 select 语句从表中显示所有记录 −
mysql> select * from DemoTable;
这将生成以下输出 −
+----------+-----------------+ | UserName | UserType | +----------+-----------------+ | Chris | New User | | David | New User | | Mike | Registered User | | Sam | New User | +----------+-----------------+ 4 rows in set (0.00 sec)
以下是获取不同类型的用户数量的查询 −
mysql> select count(*),UserType from DemoTable group by UserType;
这将生成以下输出−
+----------+-----------------+ | count(*) | UserType | +----------+-----------------+ | 3 | New User | | 1 | Registered User | +----------+-----------------+ 2 rows in set (0.00 sec)
广告