1建表语句----DLL
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) DEFAULT NULL COMMENT '年龄',
`sex` char(2) DEFAULT NULL COMMENT '性别',
`introduce` varchar(255) DEFAULT NULL COMMENT '简介',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2.插入语句----DML
insert into student values(0,'2024-02-25 10:00:00','李华','123456','15633851025',20,'男','男神');
insert into student values(0,'2024-02-25 10:00:00','李明','123456','15633851025',18,'男','男神');
insert into student values(0,'2024-02-25 10:00:00','李洋','123456','15633851025',22,'男','不知名的男性');
insert into student values(0,'2024-02-25 10:00:00','刘亦菲','123456','15633851025',20,'女','神仙姐姐');
insert into student values(0,'2024-02-25 10:00:00','周杰伦','123456','15633851025',20,'男','爱听他的歌');
insert into student (userName,age,introduce)values('张杰','37','喜欢听他的歌');
select *from student;
3.基础查询语句---DQL
#1.基础查询
select*from student;
#2.分列匿名以及筛选数据查询
select userName as'姓名',age as'年龄',sex as'性别',introduce as'简介',from student where pwd is not null;
#3.去重查询
select distinct sex'性别类型'from student;
#4.排序查询
select userName as '姓名' ,age '年龄',sex '性别',introduce'简介',from student ORDER BY age desc;
#5.分页查询.limit这是个重载函数
#1个参数的limit用法是显示多少条信息
select *from student;
select *from student limit 2;
#2个参数
select *from student limit 4,2;
4模糊查询-DQL
#like 用于模糊查询
SELECT * FROM `student`where userName like '李%';
SELECT * FROM `student`where userName like '张_';
#is null 的使用not代表否定
SELECT * FROM `student`where pwd is null;
SELECT * FROM `student`where pwd is not null;
#between and 是范围查询,能查询数值与时间范围
SELECT * FROM `student`where age between 16 and 21;
SELECT * FROM `student`where createDate between '2024-02-24 10:00:00' and '2024-02-26 10:00:00';
#in 相当于多个or来使用
SELECT * FROM `student`where userName in('李华','李明','张杰');
5聚合函数sum-avg-max-min-count
#5个常用的聚合函数count(x)计数的maxmin最大值最小值avg求平均数sum求和
#计数
select count (*)from student where pwd is not null;
#最大最小值
select max(age)'最大年龄',min (age)'最小年龄'from student;
#平均数
select avg(age)'平均年龄'from student ;
#求和
select sum(age)'总年龄'from student;
#平均数对照
select avg(age)'平均年龄',sum(age)/(select count (*)from student)'sum 计算平均年龄'from student;