56,679
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE `person` (
`id` int(11) NOT NULL,
`name` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `person` VALUES (1,'aa',20),(2,'aa',21),(3,'aa',18),(4,'aa',31);
mysql> select * from person;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | aa | 20 |
| 2 | aa | 21 |
| 3 | aa | 18 |
| 4 | aa | 31 |
+----+------+------+
4 rows in set (0.00 sec)
mysql> select sum(age>20),sum(age<=20) from person;
+-------------+--------------+
| sum(age>20) | sum(age<=20) |
+-------------+--------------+
| 2 | 2 |
+-------------+--------------+
1 row in set (0.00 sec)
mysql>
select (select COUNT(1) from person where age>20) as 大于20人数,
(select COUNT(1) from person where age<=20) as 小于20人数