From 2d8bd1e64c36ca5b8a55ae3681510c4b2464364d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=AD=90=E8=B1=AA?= <2936219414@qq.com> Date: Thu, 23 Feb 2023 22:02:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?mysql=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\254\241\344\275\234\344\270\232.md" | 221 ++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 "54 \345\217\266\345\255\220\350\261\252/mysql\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" diff --git "a/54 \345\217\266\345\255\220\350\261\252/mysql\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" "b/54 \345\217\266\345\255\220\350\261\252/mysql\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..8f310a4 --- /dev/null +++ "b/54 \345\217\266\345\255\220\350\261\252/mysql\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,221 @@ +-- ## 第1题:员工表 + +```mysql + CREATE DATABASE abc CHARSET utf8; +USE abc; +``` + +-- ```mysql +` + +```mysql +drop table if exists `employee`; +``` + +​ ` +-- #创建employee表 +` + +```mysql + CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT + ); +``` + +` +-- #添加信息 + +```mysql + 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); +SELECT*FROM employee; +``` + + +| -- | **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之间的员工信息。 + +```mysql +SELECT*FROM employee WHERE salary BETWEEN 12000 and 13000; +``` + +-- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 + +```mysql +SELECT id,`name`, addr FROM employee WHERE `NAME` LIKE '刘__' ; +``` + +-- **要求3:**将“李四”的家庭住址改为“广东韶关” + +```mysql +SELECT addr="广东韶关" FROM employee WHERE name="李四"; +``` + +-- **要求4:**查询出名字中带“小”的员工 + +```mysql +SELECT*FROM employee WHERE `NAME` LIKE '%小%'; +``` + +-- **要求5:**查询出薪资高于11000的男员工信息 + +```mysql +SELECT*FROM employee WHERE salary > 11000 AND sex = '男'; +``` + +-- **要求6:**查询没有登记电话号码的员工 + +```mysql +SELECT*FROM employee WHERE tel IS null; +``` + +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + +```mysql +SELECT*FROM employee WHERE salary > 12000 OR addr = '广东深圳' AND addr = '广州'; +``` + +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” + +```mysql +SELECT name 姓名,salary*12 年薪 FROM employee; +``` + + + +-- ```mysql +-- +-- ``` +-- +-- ## 第2题:国家信息表 +-- +-- countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```mysql +CREATE DATABASE abcd CHARSET utf8; +USE abcd; +``` + +-- ```mysql + +```mysql + DROP TABLE IF EXISTS `countries_info`; + CREATE TABLE `countries_info`( +`name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT + ); +``` + + + +```mysql + 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); +SELECT*FROM countries_info; +``` + + +-- ``` +-- +-- 表数据样例: +-- +-- ```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:** 查询大国 的国家名称、人口和面积。 +```mysql +SELECT `NAME`,continent,population FROM countries_info; +``` + +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +```mysql +SELECT*FROM countries_info WHERE area > 3000000 OR population > 25000000; +``` + + + +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- +-- - 人口至少为 2500 万(即 25000000) +-- +-- **要求2:**查询属于亚洲的国家名称、所属大陆面积、人口和GDP 值 +-- +```mysql +SELECT name,area,population,gdp FROM countries_info WHERE continent = 'Asia'; +``` + + +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 +-- +```mysql +SELECT*FROM countries_info WHERE area < 10000 AND population < 100000; +``` + +-- **要求4:**查询国家名字中包含“o“字母的国家信息 + +```mysql +SELECT*FROM countries_info WHERE name LIKE '%o%'; +``` + +-- **要求5:**查询GDP值超过10000000000的国家信息 + +```mysql +SELECT*FROM countries_info WHERE gdp > 10000000000; +``` + +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” +```mysql +SELECT gdp/area 人均gdp,name,population,gdp FROM countries_info; +``` + + + +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +-- + +```mysql +SELECT*FROM countries_info WHERE gdp/population < 1000; +``` + +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +```mysql +SELECT area/population 人均国土面积,name,area,population FROM countries_info; +``` + + + -- Gitee From eac16869c07eab893f4f3a4dbaa9d28605a295bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=AD=90=E8=B1=AA?= <2936219414@qq.com> Date: Wed, 8 Mar 2023 22:26:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?mysql=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\254\241\344\275\234\344\270\232.md" | 200 ++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 "54 \345\217\266\345\255\220\350\261\252/mysql\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" diff --git "a/54 \345\217\266\345\255\220\350\261\252/mysql\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" "b/54 \345\217\266\345\255\220\350\261\252/mysql\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..e12d2b2 --- /dev/null +++ "b/54 \345\217\266\345\255\220\350\261\252/mysql\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,200 @@ +# 笔记 + +7大字句 +1. from : 从哪些表中筛选 +2. on : 关联多表查询时,去除笛卡尔积 +3. where : 从表中筛选的条件 +4. group by : 分组依据 +5. having:在统计结果中再次筛选 +6. order by: 排序 +7. limit:分页 +必须按照(1)~(7)的顺序 编写字句 + +having子句也写条件 +where的条件是针对原表中的记录的筛选。where后面不能出现分组函数。 +having子句是对统计结果(分组函数计算后)的筛选。having可以加分组函数。 + +### 子查询 + +嵌套在其他sql语句中的查询语句 + +子查询通常先运行,所以要用()包括起来 + +子查询的运行结果可以作为其语句条件,数据源等 + +## select 1 from 2 where 3 + +# 作业 + +在如图的数据表上完成以下题目 +表一 + +~~~ sql +CREATE TABLE `stuinfo` ( + `stuN0` varchar(20) NOT NULL, + `stuName` varchar(20) DEFAULT NULL, + `stuSex` varchar(10) DEFAULT NULL, + `stuAge` int(11) DEFAULT NULL, + `stuAddress` varchar(20) DEFAULT NULL, + `stuseat` int(11) DEFAULT NULL, + PRIMARY KEY (`stuN0`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +~~~ + +表二 + +~~~ sql +CREATE TABLE `stuexam` ( + `examN0` int(11) NOT NULL, + `stuN0` varchar(20) DEFAULT NULL, + `wittenExam` int(11) DEFAULT NULL, + `labExam` int(11) DEFAULT NULL, + PRIMARY KEY (`examN0`), + KEY `stuN0` (`stuN0`), + CONSTRAINT `stuexam_ibfk_1` FOREIGN KEY (`stuN0`) REFERENCES `stuinfo` (`stuN0`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +~~~ + +表三 + +~~~ sql +CREATE TABLE `stumarks` ( + `examN0` int(11) DEFAULT NULL, + `stuID` varchar(20) DEFAULT NULL, + `score` int(11) DEFAULT NULL, + KEY `examN0` (`examN0`), + CONSTRAINT `stumarks_ibfk_1` FOREIGN KEY (`examN0`) REFERENCES `stuexam` (`examN0`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +~~~ + + +添加数据 + + +1. ~~~ sql + INSERT into stuinfo VALUES ('s2501','张秋利','男',20,'美国硅谷',1),('s2502','李斯文','女',18,'湖北武汉',2),('s2503','马文才','男',18,'湖南长沙',3),('s2504','欧阳俊雄','女',21,'湖北武汉',4),('s2505','梅超风','男',16,'湖北武汉',5),('s2506','陈旋风','男',19,'美国硅谷',6); + + ~~~sql + +2. + +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); + ~~~ + +3. + +~~~ sql +INSERT INTO stuMarks VALUES +(1,'s2501',88), +(2,'s2501',92), +(3,'s2501',53), +(4,'s2502',60), +(5,'s2502',99), +(6,'s2503',82); +~~~ + +1.查询出年龄比班上平均年龄大的学生的信息 + +~~~ sql +SELECT * from stuinfo where stuAge >(select avg(stuAge) from stuinfo); +~~~ + +2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) + +~~~ sql +select t.stuN0,t.stuName,t.stuSex,max(score) from stumarks s left join stuinfo t on s.stuID=t.stuN0 group by s.stuID; +~~~ + +3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) + +~~~ sql + +~~~ + +4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) + +~~~ sql +SELECT * from stuinfo where stuSex='男' and stuAge>=20; + +~~~ + +~~~ sql +SELECT * from stuinfo where stuAge=(SELECT stuAge from stuinfo where stuSex='男' and stuAge>=20); + +~~~ + + + +5.查询出年龄比所有男生年龄都大的女生的信息 + +~~~ sql +SELECT * from stuinfo where stuSex='女'and stuAge> all (SELECT stuAge from stuinfo where stuSex='男'); + +~~~ + + + +6.查询出所有选修课程都及格的学生的信息 (stuMarks) + +~~~ sql +SELECT * from stuinfo LEFT JOIN stumarks on stuinfo.stuN0=stumarks.stuID WHERE score>60 GROUP BY stuid; +~~~ + + + +7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +~~~ sql +SELECT * from stuinfo a LEFT JOIN stumarks s on a.stuN0=s.stuID where s.score is not null; +~~~ + + + +8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +~~~ sql +SELECT * from stuinfo a LEFT JOIN stumarks s on a.stuN0=s.stuID where s.score is null; +~~~ + + + +9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + +~~~ sql +SELECT * from stuinfo LEFT JOIN stumarks on stuinfo.stuN0=stumarks.stuID where score>90; +~~~ + + + +10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) + + + +11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) + +~~~ sql +SELECT a.* from stuinfo a LEFT JOIN stumarks s on a.stuN0=s.stuID where s.score >(SELECT score from stumarks where stuID='s2501') GROUP BY stuID; +~~~ + + + +12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) + +13.查询班上比所有男生年龄都要大的女生的信息 + +~~~ sql +SELECT * from stuinfo where stuSex='女'and stuAge> all (SELECT stuAge from stuinfo where stuSex='男'); +~~~ + +14.查询出只要比某个男生年龄大的女生的信息 + +~~~ sql +select * from stuinfo where stusex = '女' and stuage > (select min(stuage) from stuinfo where stusex = '男'); +~~~ + -- Gitee