From 03e97bd2e4da63752b91f47d564e9e9e6d92a4b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E6=AD=A3=E6=B3=A2?= <1938448998@qq.com> Date: Thu, 23 Feb 2023 20:31:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BA=8C=E6=9C=88=E4=BA=8C=E5=8D=81?= =?UTF-8?q?=E4=B8=89=E6=97=A5=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...23\350\277\220\347\256\227\347\254\246.md" | 211 ++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 "09 \346\233\271\346\255\243\346\263\242/20230223 MySQL\346\225\260\346\215\256\345\272\223\350\277\220\347\256\227\347\254\246.md" diff --git "a/09 \346\233\271\346\255\243\346\263\242/20230223 MySQL\346\225\260\346\215\256\345\272\223\350\277\220\347\256\227\347\254\246.md" "b/09 \346\233\271\346\255\243\346\263\242/20230223 MySQL\346\225\260\346\215\256\345\272\223\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000..8043d86 --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20230223 MySQL\346\225\260\346\215\256\345\272\223\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,211 @@ +# 笔记 + +select 字段列表 from 表名称; + +select 字段列表 from 表名称 where 条件; + +别名(该临时字段名) + +select 字段名1 as 别名1,字段2 as 别名2 from 表名称 as 别名; + +结果去重 distinct 只支持一个字段 + +select distinct 字段列表 from 表名称 [where 条件] + +运算符 + +div 只保留整数部分 + +例 select 9/2,9 div 2 + +结果4.5,4 + +等于=不能用于null, + +判断是null用is null或用<=>null + +判断不是null 用is not null + +查询信息 + +select * from 表名称 where 字段1 in(字段数据1,字段数据2); + +查询遇到null时 + +select ifnull(字段名,0)from 表名称; + +区间范围 + +字段名 between X and Y 包含两端的边界 + +集合范围(in) + +in(x,x,x) not in(x,x,x)不在此范围 + +查询在这字段的数据 + +select * from 表名称 where 字段名 in(字段数据) + +%代表任意字符 + +_代表一个字符 + +模糊查询用like + +# 作业 + +-- ## 第1题:员工表 +-- +-- ```mysql +-- drop table if exists `employee`; +create database yuan; +use yuan; + #创建employee表 + CREATE TABLE employee( +id INT, +`name` VARCHAR(20), +sex VARCHAR(20), +tel VARCHAR(20), +addr VARCHAR(50), +salary FLOAT + +); + +-- #添加信息 +alter database yuan character set utf8; +alter table employee convert to character set utf8; +INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); + +-- + +-- +-- +| -- | **id** | **name** | **sex** | **tel** | **addr** | **salary** | +| ------------------------------------------------------------ | ------ | -------- | ------- | ------------ | -------- | ---------- | +| -- | 10001 | 张一一 | 男 | 13456789000 | 广东韶关 | 10010.58 | +| -- | 10002 | 刘小红 | 女 | 13454319000 | 广东江门 | 12010.21 | +| -- | 10003 | 李四 | 男 | 0751-1234567 | 广东佛山 | 10040.11 | +| -- | 10004 | 刘小强 | 男 | 0755-5555555 | 广东深圳 | 15010.23 | +| -- | 10005 | 王艳 | 女 | NULL | 广东广州 | 14050.16 | +| -- | | | | | | | +| -- **要求1:**查询出薪资在12000~13000之间的员工信息。 | | | | | | | +| select * from employee where salary between 12000 and 13000; | | | | | | | +| -- | | | | | | | +| -- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 | | | | | | | +| select id,`name`,addr from employee where `name` like '刘%'; | | | | | | | +| -- | | | | | | | +| -- **要求3:**将“李四”的家庭住址改为“广东韶关” | | | | | | | +| update employee set addr='广东韶关' where `name`='李四'; | | | | | | | +| -- | | | | | | | +| -- **要求4:**查询出名字中带“小”的员工 | | | | | | | +| select * from employee where `name` like '%小%'; | | | | | | | +| -- | | | | | | | +| -- **要求5:**查询出薪资高于11000的男员工信息 | | | | | | | +| select * from employee where salary>11000; | | | | | | | +| -- | | | | | | | +| -- **要求6:**查询没有登记电话号码的员工 | | | | | | | +| select * from employee where tel is null; | | | | | | | +| -- | | | | | | | +| -- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 | | | | | | | +| select * from employee where (salary>12000 or addr='广东深圳' or addr='广东广州') and sex='男'; | | | | | | | +| -- | | | | | | | +| -- **要求8:**查询每个员工的年薪,显示“姓名、年薪” | | | | | | | +| select `name` as 姓名,salary * 12 as 年薪 from employee; | | | | | | | +| -- | | | | | | | +| -- ```mysql | | | | | | | +| -- | | | | | | | +| -- ``` | | | | | | | +| -- | | | | | | | + + + +-- ## 第2题:国家信息表 +-- +-- countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 +create database guo charset utf8; + +use guo; + +-- ```mysql +-- DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( +`name` VARCHAR(100), +`continent` VARCHAR(100), +`area` INT, +population INT, +gdp BIGINT + +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); + +-- ``` + +-- 表数据样例: + +-- ```mysql +-- +-------------+-----------+---------+------------+--------------+ +-- | name | continent | area | population | gdp | +-- +-------------+-----------+---------+------------+--------------+ +-- | Afghanistan | Asia | 652230 | 25500100 | 20343000000 | +-- | Albania | Europe | 28748 | 2831741 | 12960000000 | +-- | Algeria | Africa | 2381741 | 37100000 | 188681000000 | +-- | Andorra | Europe | 468 | 78115 | 3712000000 | +-- | Angola | Africa | 1246700 | 20609294 | 100990000000 | +-- +-------------+-----------+---------+------------+--------------+ + +-- ``` + +-- **要求1:** 查询大国 的国家名称、人口和面积。 + +select `name`,area,population from `countries_info` where area>=3000000 or population>=25000000; + +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +-- - 面积至少为 300万平方公里(即,3000000 km2) + +-- - 人口至少为 2500 万(即 25000000) + +--要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 + +select * from `countries_info` where continent='Asia'; + +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 + +select * from `countries_info` where area<10000 and population<100000; + +-- **要求4:**查询国家名字中包含“o“字母的国家信息 + +select * from `countries_info` where `name` like '%o%'; + +-- **要求5:**查询GDP值超过10000000000的国家信息 + +select * from `countries_info` where gdp>10000000000; + +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” + +select `name`,population,gdp,gdp div population as 人均贡献GDP值 from `countries_info`; + +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 + +select * from `countries_info` where (gdp div population)>1000; + +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” + +select `name` as 国家名,area as 面积,population as 人口,area / population as 人均国土面积值 from `countries_info`; + +-- ```mysql + +-- ``` +-- +-- \ No newline at end of file -- Gitee From ae00d0192f63374ed631babaaae4014f677c6b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E6=AD=A3=E6=B3=A2?= <1938448998@qq.com> Date: Wed, 1 Mar 2023 20:37:55 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BA=8C=E6=9C=88=E4=BA=8C=E5=8D=81?= =?UTF-8?q?=E5=85=AB=E6=97=A5=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...63\350\201\224\346\237\245\350\257\242.md" | 175 ++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 "09 \346\233\271\346\255\243\346\263\242/20230228 MySQL\346\225\260\346\215\256\345\272\223\345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/09 \346\233\271\346\255\243\346\263\242/20230228 MySQL\346\225\260\346\215\256\345\272\223\345\205\263\350\201\224\346\237\245\350\257\242.md" "b/09 \346\233\271\346\255\243\346\263\242/20230228 MySQL\346\225\260\346\215\256\345\272\223\345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000..7d3d570 --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20230228 MySQL\346\225\260\346\215\256\345\272\223\345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,175 @@ +# 笔记 + +Avg()平均值 Sum()求和 Max()最大值 + +Min()最小值 count()统计记录器 + +count(*)统计全部,count*(字段)统计非null的值 + +关联查询 + +内连接:a inner join b on a.字段=b.字段 ab相同数据 + +左全连接:a left join b on a.字段=b.字段 where a.字段 is null 不重复的a数据 + +右全连接:a right join b on a.字段=b.字段 where b.字段 is null 不重复的b数据 + +左连接:a left join b on a.字段=b.字段 全a数据 + +右连接:a right join b on a.字段=b.字段 全b数据 + + + +# 作业 + +```mysql +## 创建添加数据顺序 1,4,2,3 + +create database glcx charset utf8; +use glcx; +create table Student( +Sno varchar (20) not null primary key, +Sname varchar (20) not null, +Ssex varchar (20) not null, +Sbirthday datetime, +Class varchar (20) +); +create table Course( +Cno varchar (20) not null primary key, +Cname varchar (20) not null, +Tno varchar (20) not null, +foreign key(Tno) references Teacher(Tno) +); +create table Score( +Sno varchar (20) not null, +foreign key(Sno) references Student(Sno), +Cno varchar (20), +foreign key(Cno) references Course(Cno), +Degree Decimal(4,1) +); +create table Teacher( +Tno varchar (20) not null primary key, +Tname varchar (20) not null, +Tsex varchar (20) not null, +Tbirthday datetime, +Prof varchar (20), +Depart varchar (20) not null +); +insert into Student values +(108,'曾华','男','1977-9-1',95033), +(105,'匡明','男','1975-10-2',95031), +(107,'王丽','女','1976-1-23',95033), +(101,'李军','男','1976-2-20',95033), +(109,'王芳','女','1975-2-10',95031), +(103,'陆君','男','1974-6-3',95031); +insert into Course VALUES +('3-105','计算机导论',825), +('3-245','操作系统',804), +('6-166','数字电路',856), +('9-888','高等数学',831); +insert into Score VALUES +(103,'3-245',86), +(105,'3-245',75), +(109,'3-245',68), +(103,'3-105',92), +(105,'3-105',88), +(109,'3-105',76), +(101,'3-105',64), +(107,'3-105',91), +(108,'3-105',78), +(101,'6-166',85), +(107,'6-166',79), +(108,'6-166',81); +insert into Teacher VALUES +(804,'李诚','男','1958-12-2','副教授','计算机系'), +(856,'张旭','男','1969-3-12','讲师','电子工程系'), +(825,'王萍','女','1972-5-5','助教',' 计算机系'), +(831,'刘冰','女','1977-8-14','助教',' 电子工程系'); + +-- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +表1关联表3,表3关联表2 +select * from Student inner join Score inner join Course on +Student.Sno=Score.Sno and Score.Cno=Course.Cno; +-- 2,查询没有学生的教师的所有信息 + +-- 1.查询Score表中的最高分的学生学号和课程号。 +select Sno,Cno from Score; +-- 2.查询所有学生的Sname、Cno和Degree列。 +select Student.Sname,Score.Cno,Score.Degree from +Score inner join Student on Student.Sno=Score.Sno; +-- 3.查询所有学生的Sno、Cname和Degree列。 +select Score.Sno,Course.Cname,Score.Degree FROM +Course inner join Score on Course.Cno=Score.Cno; +-- 4.查询所有学生的Sname、Cname和Degree列。 +select Student.Sname,Course.Cname,Score.Degree FROM +Student inner join Score inner join Course +on Score.Sno=Student.Sno and Score.Cno=Course.Cno; +-- 5.查询“95033”班学生的平均分。 +select round(sum(Degree) / count(Class),1) 成绩 from Student inner join Score on +Score.Sno=Student.Sno where Class='95033'; +-- 6.查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from Score where Cno='3-105' and degree>(sno='109'); +-- 7.查询score中选学多门课程的同学中分数为非最高分成绩的记录。 + +-- 8.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from Student inner join Score inner join Course on +Student.Sno=Score.Sno and Score.Cno=Course.Cno where +(Score.Sno='109') (select avg(Degree) from Score); +-- 17.查询所有任课教师的Tname和Depart. +select tname,Depart from Teacher inner join Course on +Course.Tno=Teacher.Tno +where +Teacher.Tno = Course.Tno; +-- 18.查询所有未讲课的教师的Tname和Depart. +select tname,Depart from Teacher inner join Course on +Course.Tno=Teacher.Tno +where +not (Teacher.Tno = Course.Tno); +-- 19.查询“男”教师及其所上的课程。 +select Tname,Cname from Teacher inner join Course on +Teacher.Tno=Course.Tno +where +Tsex='男'; +-- 20.查询最高分同学的Sno、Cno和Degree列。 +select sno,cno,Degree from Score +WHERE +(select max(Degree) from Score); +-- 21.查询和“李军”同性别的所有同学的Sname. +select sname from Student +where +Ssex=(select Ssex from Student where Sname='李军'); +-- 22.查询和“李军”同性别并同班的同学Sname. +select sname from Student +where +Ssex=(select Ssex from Student where Sname='李军') +and +Class=(select Class from Student where Sname='李军'); +-- 23.查询所有选修“计算机导论”课程的“男”同学的成绩表. +select Degree from Course inner join Score inner join Student on +Course.Cno=Score.Cno and Score.Sno=Student.Sno +where +Ssex='男' +and +Cname=(select Cname from Course where Cname='计算机导论') ; +``` + -- Gitee From 7d0e1dc270c1d180c19a6780eec67cfb422169b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E6=AD=A3=E6=B3=A2?= <1938448998@qq.com> Date: Wed, 8 Mar 2023 13:38:23 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=B8=89=E6=9C=88=E4=B8=83=E5=8F=B7?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...23\345\255\220\346\237\245\350\257\242.md" | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 "09 \346\233\271\346\255\243\346\263\242/20230307 MySQL\346\225\260\346\215\256\345\272\223\345\255\220\346\237\245\350\257\242.md" diff --git "a/09 \346\233\271\346\255\243\346\263\242/20230307 MySQL\346\225\260\346\215\256\345\272\223\345\255\220\346\237\245\350\257\242.md" "b/09 \346\233\271\346\255\243\346\263\242/20230307 MySQL\346\225\260\346\215\256\345\272\223\345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..d89a55a --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20230307 MySQL\346\225\260\346\215\256\345\272\223\345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,118 @@ +# 笔记 + +单列单值可以放在select、where、having后面 + +多列多值只能放在where后面,用in、not in进行比较 + +# 作业 + +``` mysql +create database lianxi charset utf8; +use lianxi; +drop table stuinfo; +drop table stuExam; +create table stuinfo( +stuNO varchar(10) primary key, +stuName varchar(10), +stuSex char, +stuage int(3), +stuAddress varchar(5), +stuSeat int(2) +); +create table stuExam( +examNO int(2) primary key, +stuNO varchar(10), +foreign key(stuNO) references stuinfo(stuNO), +writtenExam int(3), +labExam int(3) +); +create table stuMarks( +examNO int(2), +foreign key(examNO) references stuExam(examNO), +stuID varchar(10), +score int(3) +); +insert into stuinfo VALUES +('s2501','张秋利','男',20,'美国硅谷',1), +('s2502','李斯文','女',18,'湖北武汉',2), +('s2503','马文才','男',18,'湖南长沙',3), +('s2504','欧阳俊雄','女',21,'湖北武汉',4), +('s2505','梅超风','男',16,'湖北武汉',5), +('s2506','陈旋风','男',19,'美国硅谷',6); +insert into stuExam values +(1,'s2501',50,70), +(2,'s2502',60,65), +(3,'s2503',86,70), +(4,'s2504',40,80), +(5,'s2505',70,85), +(6,'s2506',85,90); +insert into stuMarks values +(1,'s2501',88), +(2,'s2501',92), +(3,'s2501',53), +(4,'s2502',60), +(5,'s2502',99), +(6,'s2503',82); +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuage>(select avg(stuage) from stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select a.stuNO,stuName,stuSex,score +from stuinfo a inner join stuMarks c on +a.stuNO=c.stuID +where score in (select max(score) from stuMarks group by stuID); +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuNO,a.stuName,a.stuSex,(writtenExam+labExam)/2 +from stuinfo a left join stuExam b ON +a.stuNO=b.stuNO +where (writtenExam+labExam)/2 in (select (writtenExam+labExam)/2 from stuExam group by examNO); +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex='男' and stuage>=20; + +select * from stuinfo +where stuSex in (select stuSex from stuinfo where stuSex='男') +and +stuage in (select stuage from stuinfo where stuage>=20); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuage in (select max(stuage) from stuinfo where stuSex='女' group by stuSex); +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stuinfo a inner join stuExam b on +a.stuNO=b.stuNO +where b.writtenExam in (select b.writtenExam from stuExam where b.writtenExam>=60) +AND +b.labExam in (select b.labExam from stuExam where b.labExam>=60); +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a inner join stuExam b inner join stuMarks c ON +a.stuNO=b.stuNO and b.examNO=c.examNO +where a.stuNO in (select stuID from stuMarks group by stuID); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a inner join stuExam b inner join stuMarks c ON +a.stuNO=b.stuNO and b.examNO=c.examNO +where a.stuNO not in (select stuID from stuMarks group by stuID); +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.* from stuinfo a inner join stuMarks b on +a.stuNO=b.stuID +where score in (select score from stuMarks where score>=90 group by stuID); +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select a.* from stuinfo a inner join stuMarks b on +a.stuNO=b.stuID +where score in (select avg(score) from stuMarks group by stuID) group by 'avg(score)'; +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select a.* from stuinfo a inner join stuMarks b on +a.stuNO=b.stuID +where +(select sum(score) from stuMarks where stuID='s2501') in +(select sum(score) from stuMarks where stuID!='s2501' group by stuID); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select a.* from stuinfo a inner join stuMarks b on +a.stuNO=b.stuID +where score in (select score from stuMarks where score in +(select min(score) from stuMarks where stuID='s2501') < +(select score from stuMarks where stuID!='s2501' group by score) +); +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuage in (select max(stuage) from stuinfo where stuSex='女' group by stuSex); +-- 14.查询出只要比某个男生年龄大的女生的信息 +``` + + + -- Gitee