如何在MySQL中找到出生日期在1980年到1996年之间的用户百分比?
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> DateOfBirth varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019/01/31'); mysql> insert into DemoTable values('1980/02/01'); mysql> insert into DemoTable values('1985/04/10'); mysql> insert into DemoTable values('1995/06/04'); mysql> insert into DemoTable values('1990/12/24'); mysql> insert into DemoTable values('1996/11/04');
使用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)