【无标题】分组查询————GROUP BY

本文详细介绍了如何在名为schoolDB的数据库中创建表,执行插入操作,并展示了一系列SQL查询,包括基本查询、使用聚合函数、分组查询等,涉及DDL(数据定义语言)、DML(数据操纵语言)和DQL(数据查询语言)。
摘要由CSDN通过智能技术生成

  前言

数据库名称【schoolDB】,字符集【utf8_general_ci】。

1.建表语句——DDL

CREATE TABLE `student` (
  `id` int(11) NOT NULL COMMENT '学号',
  `creatDate` datetime DEFAULT NULL COMMENT '创建时间',
  `userName` varchar(20) DEFAULT NULL COMMENT '用户名',
  `pwd` varchar(36) DEFAULT NULL COMMENT '密码',
  `phone` varchar(11) DEFAULT NULL COMMENT '手机号',
  `age` tinyint(3) DEFAULT NULL COMMENT '年龄',
  `sex` char(2) DEFAULT NULL COMMENT '性别',
  `introduce` varchar(255) DEFAULT NULL COMMENT '简介',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 2.插入语句——DML

insert into student values(0,'2024-02-25 10:00:00','刘德华','123654','15612345678',62'男','永远的男神,爱你一万年,爱你经得起考验。');
insert into student values(0,'2024-02-25 10:00:00','刘青云','123154','15612345678',65,'男','真正的演员,好角色用心演绎。');
insert into student values(0,'2024-02-25 10:00:00','周星驰','123624','15612345678',61,'男','所有电影都没有对爱情有过轻视。');
insert into student values(0,'2024-02-25 10:00:00','张翰','122654','15612345678',32,'男','一起坐死在流星雨下。');
insert into student values(0,'2024-02-25 10:00:00','王祖贤','223654','15612345678',27,'女','你再美你能比过王祖贤吗?');
insert into student (userName,age,introduce)values('刘亦菲',37,'神仙姐姐');

3.基础查询语句—— DQL

select * from student;
select userName as'姓名',age 年龄,sex as'性别',introduce'简介'
from student
where pwd is not null;
select distinct sex '性别类型' from student;
select userName as '姓名',age 年龄,sex'性别',introduce'简介' from student ORDER BY age desc;
select * from student LIMIT 2;
select * from student limit 4,2;

4.聚合函数使用方法

select count(*) from student where pwd is not null;

select max(age) '最大年龄',min(age)'最小年龄' from student;

select avg(age) '平均年龄' from student;

select avg(age) '平均年龄',sum(age)/(select count(*) from student)'sum计算平均年龄' from student;

5.分组查询----GROUP BY

CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
  `createDate` datetime DEFAULT NULL,
  `userName` varchar(20) DEFAULT NULL,
  `pwd` varchar(36) DEFAULT NULL,
  `phone` varchar(11) DEFAULT NULL,
  `age` tinyint(3) unsigned DEFAULT NULL,
  `sex` char(2) DEFAULT '男',
  `introduce` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
 
CREATE TABLE `result` (
  `rid` int(11) NOT NULL AUTO_INCREMENT COMMENT '成绩编号',
  `testName` varchar(255) DEFAULT NULL COMMENT '测试名称',
  `score` double(4,2) DEFAULT NULL COMMENT '成绩',
  `studentId` int(11) DEFAULT NULL COMMENT '学生id',
  PRIMARY KEY (`rid`),
  KEY `studentId` (`studentId`),
  CONSTRAINT `result_ibfk_1` FOREIGN KEY (`studentId`) REFERENCES `student` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;





insert into student values(0,'2024-02-25 10:10:10','赵灵儿','123',
'15612345678',16,'女','逍遥哥哥,你终于找到我了。');
insert into student values(0,'2024-02-25 10:10:10','王语嫣','123',
'15612345678',17,'女','慕容复,我和你不共戴天。');
insert into student values(0,'2024-02-25 10:10:10','龙姑娘','123',
'15612345678',22,'女','我想过过过儿过过的日子。');
insert into student values(0,'2024-02-25 10:10:10','杨过','123',
'15612345678',18,'男','一遇杨过误终身。');
insert into student values(0,'2024-02-25 10:10:10','杨逍','123',
'15612345678',27,'男','杨过跟程英的大儿子。');
insert into student (userName,age,introduce)values('黄衣女子',26,'杨过与龙姑娘的大女儿。');
 
insert into result values(0,'政治',96,1);
insert into result values(0,'地理',86,5);
insert into result values(0,'政治',77,3);
insert into result values(0,'英语',99,3);
insert into result values(0,'历史',22,6);
insert into result values(0,'化学',69,2);
insert into result values(0,'Java',79,5);
insert into result values(0,'Java',67,6);
insert into result values(0,'政治',97,2);
insert into result values(0,'物理',76,6);
insert into result values(0,'物理',82,2);
insert into result values(0,'化学',56,4);





select testName,count(testName),sum(score),avg(score) from result GROUP BY testName;

select studentId,
count(studentId)'学生考试科目数量',
sum(score)'学生的总成绩',avg(score)'学生的平均分'
from result GROUP BY studentId;

select userName,testName,score from result,student
where result.studentId=student.id;
select userName,
count(score) '参与考试数量',
avg(score)'每人平均分数',
sum(score)'每人的总分'
from result,student
where result.studentId=student.id
and score>60
group by userName;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值