插入表
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
`createDate` 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) unsigned DEFAULT NULL COMMENT '年龄',
`sex` char(2) DEFAULT NULL COMMENT '性别',
`introduce` varchar(255) DEFAULT NULL COMMENT '简介',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
![](https://i-blog.csdnimg.cn/blog_migrate/5f6d1d4c32fe99d91b034ed49e1bc8dc.png)
新建查询
insert into student values(0,'2024-02-25 10:10:10','盘古',
'123456','15612345678',255,'神','开天辟地第一人。');
insert into student values(0,'2024-02-25 10:10:10','女娲',
'123456','15612345678',200,'女','多次造人最终成功。');
insert into student values(0,'2024-02-25 10:10:10','伏羲',
'123456','15612345678',215,'神','先天八卦');
insert into student values(0,'2024-02-25 10:10:10','神农',
'123456','15612345678',155,'男','尝百草,死与断肠草下。');
insert into student values(0,'2024-02-25 10:10:10','嫘祖',
'123456','15612345678',115,'神','教会百姓养蚕织布');
insert into student values(0,'2024-02-25 10:10:10','蚩尤',
'123456','15612345678',155,'男','锻造大神,坐下食铁巨兽。');
insert into student (userName,age,introduce) values('仓颉',125,'中国象形文字');
SELECT*FROM student
![](https://i-blog.csdnimg.cn/blog_migrate/24867b129d72f37492752008be999dd6.png)
模糊查询
#like查询
SELECT*FROM student WHERE introduce like '%人%';
#in null空查询
SELECT*FROM student WHERE pwd is NULL;
#between区域查询
SELECT*FROM student WHERE age BETWEEN 150 AND 200;
SELECT*FROM student WHERE createDate between '2024-02-20 00:00:00' and'2024-02-24 11:00:00';
SELECT*FROM student WHERE createDate >'2024-02-20 00:00:00' and createDate<'2024-02-24 11:00:00';
#in关键字查询
SELECT*FROM student WHERE userName IN('女娲','蚩尤','伏羲');
SELECT*FROM student WHERE userName NOT IN('女娲','蚩尤','伏羲');
聚合函数
#聚合函数会涉及到5个单词:数量(count)最大(max)最小(min)平均数(avg)求和(sum)
SELECT count(*)'数据总条数'FROM student where pwd is not null;
SELECT max(age)'年龄最大'FROM student;
SELECT min(age)'年龄最小'FROM student;
SELECT avg(age)'平均年龄'FROM student;
SELECT sum(age)'年龄总和'FROM student;
#适用年龄综合计算一下平均数
select sum(age)/(SELECT count(*)'数据总条数'FROM student)'平均年龄'FROM student;