如何找出在 MySQL 中出生日期介于 1980 年和 1996 年之间的用户的百分比?
我们首先创建一个表 −
mysql> create table DemoTable -> ( -> DateOfBirth varchar(100) -> ); Query OK, 0 rows affected (0.55 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('2019/01/31'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('1980/02/01'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('1985/04/10'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('1995/06/04'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('1990/12/24'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('1996/11/04'); Query OK, 1 row affected (0.19 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+-------------+ | DateOfBirth | +-------------+ | 2019/01/31 | | 1980/02/01 | | 1985/04/10 | | 1995/06/04 | | 1990/12/24 | | 1996/11/04 | +-------------+ 6 rows in set (0.00 sec)
以下是查询出生日期介于 1980 年和 1996 年之间的用户的百分比 −
mysql> SELECT Sum(DateOfBirth BETWEEN '1980/01/01' AND '1996/12/31') * 100 / Count(*) as Percentage from DemoTable;
输出
+------------+ | Percentage | +------------+ | 83.3333 | +------------+ 1 row in set (0.00 sec)
广告