-- 班级人数表 create table tb_people( id int unsigned primary key auto_increment comment 'ID', name varchar(10) not null comment '班级', gender tinyint unsigned comment '性别, 1: 男, 2: 女', pcount int unsigned comment '人数' ) comment '班级人数表'; insert into tb_people (id, name, gender, pcount)values (null,'三年级一班',1,34), (null,'三年级一班',2,12), (null,'三年级二班',1,56), (null,'三年级二班',2,14), (null,'三年级三班',1,22), (null,'三年级三班',2,43), (null,'三年级四班',1,26), (null,'三年级四班',2,38), (null,'四年级一班',1,45), (null,'四年级一班',2,13), (null,'四年级二班',1,18), (null,'四年级二班',2,27), (null,'四年级三班',1,19), (null,'四年级三班',2,26);
时间: 2024-03-14 13:45:36 浏览: 51
程序设计-建立我的班级链表.pptx
好的,你已经创建了一个班级人数表,并插入了一些数据。如果你需要查询班级人数表中的数据,你可以使用如下的SQL语句:
1. 查询所有数据:
```sql
SELECT * FROM tb_people;
```
2. 查询某个班级的所有数据:
```sql
SELECT * FROM tb_people WHERE name = '三年级一班';
```
3. 查询男女生人数:
```sql
SELECT name, SUM(CASE WHEN gender = 1 THEN pcount ELSE 0 END) AS male_count, SUM(CASE WHEN gender = 2 THEN pcount ELSE 0 END) AS female_count FROM tb_people GROUP BY name;
```
4. 查询某个班级的男女生人数:
```sql
SELECT name, SUM(CASE WHEN gender = 1 THEN pcount ELSE 0 END) AS male_count, SUM(CASE WHEN gender = 2 THEN pcount ELSE 0 END) AS female_count FROM tb_people WHERE name = '三年级一班' GROUP BY name;
```
希望这些查询语句能够帮助你进行数据分析和统计。如果你有其他的问题,可以继续问我。
阅读全文