-- 1.创建表格create table `student`(`id` int,`name` varchar(10),`sex` char(1),`age` int,`deptid` int,`height` int,`login_date` date,`course_id` int);-- 2.插入insert into student values(5,'Jim','m',21,4,159,'2021-03-02',4);insert into student values(6,'John','m',22,3,170,'2021-03-11',1);insert into student values(7,'Lily','f',20,2,180,'2021-03-11',1);insert into student values(8,'Susan','f',25,2,178,'2021-03-11',1);insert into student values(9,'Thpmas','m',24,1,168,'2021-03-11',2);insert into student values(10,'Tom','m',23,3,168,'2021-03-09',3);insert into student values(11,'若曦','f',23,1,173,'2021-03-07',3);insert into student values(12,'浩然','m',21,3,176,'2021-03-07',3),(13,'若熙','f',23,1,175,'2021-03-06',3),(14,'若溪','f',21,2,159,'2021-03-05',4),(15,'昊然','m',29,2,179,'2021-03-01',4),(16,'皓然','m',29,3,176,'2021-03-02',5),(17,'子轩','f',28,3,171,'2021-03-04',4),(18,'紫萱','f',27,4,172,'2021-03-03',5);-- 解释 *,所有数据查询select * from student;# 部分数据查询select name,height from student;-- 3.去重查询select distinct age from student;-- 3.1 组合去重select distinct age,name from student;-- 3.2 通配符去重select distinct * from student;-- 3.3 去重计数select count(distinct name,age) as num from student;-- 3.3.1 不去重计数select count(age) as num from student;-- 3.3.2 通配符不去重计数select count(*) as num from student;-- 记数countselect count(1) from student group by sex;-- 4.设置别名select s.name as 名字,s.age as 年龄 from student as s;-- 5.限制查询结果条数select * from student limit 3,5;select * from student limit 3;-- 6.查询结果排序select * from student order by height;select * from student order by height desc;-- 6.1查询结果组合排序select * from student order by age,height desc;-- 7.条件查询# 单一指向select * from student where height=170;select name,height from student where height=170;# 比较运算符select name,height from student where height>=170;# -多个查询条件-逻辑运算符select * from student where height > 170 and age < 25;# 使用like '%'模糊查询select * from student where name like 'T%';# 不区分大小写,区分大小写加binaryselect * from student where name like binary 'T%';# 使用like '_',占位符select * from student where name like 'L___';# 区间select * from student where age between 20 and 21;# 为空和不为空,is null和is not nullselect * from student where login_date is null;
通常group by和聚合函数使用
1.如果没有使用聚合函数,只显示每个分组第一条记录
聚合函数
- • COUNT() 统计记录的条数;
- • SUM() 计算字段值的总和;
- • AVG() 计算字段值的平均值;
- • MAX() 查询字段的最大值;
- • MIN() 查询字段的最小值。
- • group_concat() 字符拼接函数
```sql
— 8.group by
单独使用group by(没有聚合函数),只显示每个分组第一条记录
select name,sex from student group by sex;
sql标准,字段分组需要显示
select sex from student group by sex;
聚合函数-计数: ()知只是表示拿什么字段计数
select sex,count(1) from student group by sex; select age,sex,count(1) from student group by sex,age;
去重
select age,sex,count(distinct 1) from student group by sex,age;
聚合函数-拼接,把女的分为一组显示,男的分为一组显示
— 通常用法:分组字段显示出来,然后组内实现某个函数 select sex,group_concat(name) from student group by sex;
```sql-- 9.having:过滤分组-- having对select字段过滤,where对from字段过滤select name,sex,height from student having height > 150;select name,sex,height from student where height > 50;-- having可以使用别名,where字段不可以使用别名select name,sex,height from student having height > 172;select name,sex,height as h from student having h > 172;# 一般与聚合函数一起使用# 执行顺序:group对from字段进行分组,把名字穿起来,求平均身高,筛选平均身高不能大于170select group_concat(name),sex,avg(height) from student group by sex having avg(height) > 171;
create table course(course_id int,course_name varchar(10));insert into course values(1,'Java'),(2,'C语言'),(3,'MySQL'),(4,'Spark'),(5,'Hadoop');alter table student add column course_id int;-- 笛卡尔积select * from student,course;# 无意义的笛卡尔积select id,name,sex,s.course_id,course_name from student s,course c;# 有意义的笛卡尔积,重复的字段来源指定从表,否则会报错# 交叉链接cross joinselect id,name,sex,s.course_id,course_name from student s,course c where s.course_id = c.course_id;# 内链接inner joinselect id,name,sex,s.course_id,course_name from student s inner join course c on s.course_id = c.course_id;# 外连接left joinselect s.name,c.course_name from student s left join course c on s.course_id=c.course_id;update student set course_id=6 where id=18;select s.name,c.course_name from student s left join course c on 1=1;select s.name,c.course_name from student s left join course c on 1=1 where s.course_id=c.course_id;# left join on andselect s.name,c.course_name from student s left join course c on s.course_id=c.course_id and c.course_name= 'MySQL';# left join on whereselect s.name,c.course_name from student s left join course c on s.course_id=c.course_id where c.course_name= 'MySQL';
子查询
-- 子查询-- 双重where筛选:学生where id筛选,课程表where id筛选select name from student where course_id in(select course_id from course where course_name = 'Java');
插入
-- 插入insert into,两种插入数据方式# insert into tbn values()和insert into tbnNew select coln from tbn;create table tb_courses(course_id int auto_increment,course_name char(40),course_grade float,course_info char(100),primary key(course_id));insert into tb_courses(course_name,course_grade,course_info) values('Network',3,'Computer Network');insert into tb_courses(course_name,course_grade,course_info) values('Daabase',3,'MySQL');insert into tb_courses(course_name,course_grade,course_info) values('System',3,'Operation');create table tb_course_new(course_id int auto_increment,course_name char(40),course_grade float,course_info char(100),primary key(course_id));## 第二种:insert into tbnNew select coln from tbn;insert into tb_course_new select course_id,course_name,course_grade,course_info from tb_courses;### 第三种插入:建表插入(缺少约束):用于备份create table as--表格查询出数据直接create table tb_course2 as select * from tb_courses;
修改
-- update# 列值全部修改update tb_course_new set course_grade = 4;# 指定列值修改update tb_course_new set course_grade = 5 where course_info = 'MySQL';
删除
-- delete# 删除表数据delete from tb_course_new;# 删除指定行数据delete from tb_course_new where course_info = 'MySQL';
union 和union all 并集
前者:对两个结果集进行并集操作,不包括重复数据,按照默认规则排序
后者:对两个结果集进行并集操作,包括重复数据,不进行排序
合并多个select语句的结果集,需要满足如下条件:
相同的列
列必须是相似的数据类型
select语句的列顺序必须一致
-- 功夫课程表create table kongfu(name varchar(10),course varchar(20),tel char(11),teacher varchar(12));insert into kongfu values('令狐冲','紫霞秘籍','13245687985','岳不群'),('张无忌','九阳真经','13556151544','白猿'),('周芷若','九阴白骨爪','16845543516','倚天屠龙记');-- 音乐课程表create table music(name varchar(10),course varchar(20),tel char(11),teacher varchar(12));insert into music values('令狐冲','笑傲江湖曲','13245687985','任盈盈'),('乐之扬','杏花天影','19838745464','乐韶凤'),('黄蓉','碧海潮生曲','16578943541','黄药师');# 并集--纵向并集select name 学生名字,tel 联系电话,course 课程名字,teacher 老师名字 from kongfuunionselect name 学生名字,tel 联系电话,course 课程名字,teacher 老师名字 from music;+----------+-------------+------------+------------+| 学生名字 | 联系电话 | 课程名字 | 老师名字 |+----------+-------------+------------+------------+| 令狐冲 | 13245687985 | 紫霞秘籍 | 岳不群 || 张无忌 | 13556151544 | 九阳真经 | 白猿 || 周芷若 | 16845543516 | 九阴白骨爪 | 倚天屠龙记 || 令狐冲 | 13245687985 | 笑傲江湖曲 | 任盈盈 || 乐之扬 | 19838745464 | 杏花天影 | 乐韶凤 || 黄蓉 | 16578943541 | 碧海潮生曲 | 黄药师 |+----------+-------------+------------+------------+select name 学生名字,tel 联系电话 from kongfuunionselect name 学生名字,tel 联系电话 from music;+----------+-------------+| 学生名字 | 联系电话 |+----------+-------------+| 令狐冲 | 13245687985 || 张无忌 | 13556151544 || 周芷若 | 16845543516 || 乐之扬 | 19838745464 || 黄蓉 | 16578943541 |+----------+-------------+# 交集--横向交集select k.name 学生名字,k.tel 联系电话 from kongfu kleft join music m on k.name = m.nameand k.tel = m.tel;+----------+-------------+| 学生名字 | 联系电话 |+----------+-------------+| 令狐冲 | 13245687985 || 张无忌 | 13556151544 || 周芷若 | 16845543516 |+----------+-------------+# 交集--横向交集select k.name 学生名字,k.tel 联系电话 from kongfu kleft join music m on k.name = m.nameand k.tel = m.tel;+----------+-------------+| 学生名字 | 联系电话 |+----------+-------------+| 令狐冲 | 13245687985 || 张无忌 | NULL || 周芷若 | NULL |+----------+-------------+select k.name 学生名字,k.tel 联系电话 from kongfu kleft join music m on k.name = m.namewhere k.tel = m.tel;+----------+-------------+| 学生名字 | 联系电话 |+----------+-------------+| 令狐冲 | 13245687985 |+----------+-------------+
