建表语句
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createDate` datetime DEFAULT NULL,
`userName` varchar(20) DEFAULT NULL,
`pwd` varchar(36) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`age` tinyint(3) DEFAULT NULL,
`sex` char(2) DEFAULT NULL,
`introduce` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
插入数据
insert into student values(0,'2024-02-25 10:00:00','赵灵儿','123',
'5156154154',16,'女','逍遥');
insert into student values(0,'2024-02-25 10:00:00','王于','123',
'5156154154',17,'女','慕容');
insert into student values(0,'2024-02-25 10:00:00','龙','123',
'5156154154',22,'女','我我我');
insert into student values(0,'2024-02-25 10:00:00','杨过','123',
'5156154154',18,'男','一遇');
insert into student values(0,'2024-02-25 10:00:00','杨潇','123',
'5156154154',27,'男','养过');
insert into student (userName,age,introduce)values('黄衣',26,
'杨过与龙姑娘的大女');
查询语句
# 聚合函数5个,数量count(x),最大值max(x),最小值min(x),平均数avg(x),求和sum(x)
SELECT COUNT(*) '所有成年人数量' FROM student WHERE age>=18;
SELECT max(age) '最大年龄', min(age) '最小年龄' FROM student;
SELECT avg(age) '平均年龄' FROM student;
SELECT sum(age) '总年龄' FROM student;
SELECT avg(age) 'avg出的总年龄'SUM(age)/(SELECT count(*) FROM student) '计算出的' FROM student;