diff --git "a/06 \351\231\210\345\277\227\344\274\237/\345\221\275\344\273\244\350\241\214.md" "b/06 \351\231\210\345\277\227\344\274\237/20230220 \345\221\275\344\273\244\350\241\214.md" similarity index 100% rename from "06 \351\231\210\345\277\227\344\274\237/\345\221\275\344\273\244\350\241\214.md" rename to "06 \351\231\210\345\277\227\344\274\237/20230220 \345\221\275\344\273\244\350\241\214.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/11\343\200\201DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.md" "b/06 \351\231\210\345\277\227\344\274\237/20230220 \350\257\255\345\217\245\347\273\203\344\271\240.md" similarity index 100% rename from "06 \351\231\210\345\277\227\344\274\237/11\343\200\201DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.md" rename to "06 \351\231\210\345\277\227\344\274\237/20230220 \350\257\255\345\217\245\347\273\203\344\271\240.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/\345\237\272\346\234\254\346\246\202\345\277\265.md" "b/06 \351\231\210\345\277\227\344\274\237/20230221 \345\237\272\346\234\254\346\246\202\345\277\265.md" similarity index 100% rename from "06 \351\231\210\345\277\227\344\274\237/\345\237\272\346\234\254\346\246\202\345\277\265.md" rename to "06 \351\231\210\345\277\227\344\274\237/20230221 \345\237\272\346\234\254\346\246\202\345\277\265.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/2\343\200\201DDL\345\222\214DML_\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" "b/06 \351\231\210\345\277\227\344\274\237/20230222 \345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" similarity index 100% rename from "06 \351\231\210\345\277\227\344\274\237/2\343\200\201DDL\345\222\214DML_\345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" rename to "06 \351\231\210\345\277\227\344\274\237/20230222 \345\273\272\350\241\250\344\270\216\345\241\253\345\200\274.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/\347\272\246\346\235\237.md" "b/06 \351\231\210\345\277\227\344\274\237/20230222 \347\272\246\346\235\237.md" similarity index 100% rename from "06 \351\231\210\345\277\227\344\274\237/\347\272\246\346\235\237.md" rename to "06 \351\231\210\345\277\227\344\274\237/20230222 \347\272\246\346\235\237.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/\344\275\234\344\270\2323_\345\237\272\347\241\200\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" "b/06 \351\231\210\345\277\227\344\274\237/20230223 \344\275\234\344\270\2323_\345\237\272\347\241\200\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" similarity index 100% rename from "06 \351\231\210\345\277\227\344\274\237/\344\275\234\344\270\2323_\345\237\272\347\241\200\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" rename to "06 \351\231\210\345\277\227\344\274\237/20230223 \344\275\234\344\270\2323_\345\237\272\347\241\200\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/20230228 \345\205\263\350\201\224\346\237\245\350\257\242.md" "b/06 \351\231\210\345\277\227\344\274\237/20230228 \345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..4282f434a177576366bf31888fcf03dc68ceffb9 --- /dev/null +++ "b/06 \351\231\210\345\277\227\344\274\237/20230228 \345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,325 @@ +## 关联查询 + +#### 分组函数 + +AVG(x):求平均值 + +SUM(x):求总和 + +MAX(x):求最大值 + +MIN(x):求最小值 + +COUNT(x):统计记录数 + +count(*)是对满足条件的(如果没有where条件,就是对所有记录)记录累加数量 + +count(常量值)等价于count(*) + +count(字段名/表达式) 只统计非NULL值的记录数 + +#### 单行函数 + +| 函数 | 用法 | +| ------------- | ------------------------------------------------------------ | +| ABS(x) | 返回x的绝对值 | +| CEIL(x) | 返回大于x的最小整数值 | +| FLOOR(x) | 返回小于x的最大整数值 | +| MOD(x,y) | 返回x/y的模 | +| RAND() | 返回0~1的随机值 | +| ROUND(x,y) | 返回参数x的四舍五入的有y位的小数的值 | +| TRUNCATE(x,y) | 返回数字x截断为y位小数的结果 | +| FORMAT(x,y) | 强制保留小数点后y位,整数部分超过三位的时候以逗号分割,并且返回的结果是文本类型的 | +| SQRT(x) | 返回x的平方根 | +| POW(x,y) | 返回x的y次方 | + +### 关联查询的SQL有几种情况 + +1、内连接:inner join ... on + +结果:A表 ∩ B表 + +2、左连接:A left join B on + +(2)A表全部 + +(3)A表- A∩B + +3、右连接:A right join B on + +(4)B表全部 + +(5)B表-A∩B + +4、全外连接:full outer join ... on,但是mysql不支持这个关键字,mysql使用union(合并)结果的方式代替 + +(6)A表∪B表: (2) A表结果 union (4)B表的结果 + +(7)A∪B - A∩B (3)A表- A∩B结果 union (5)B表-A∩B结果 + +1.内连接 + +```mysql +#演示内连接,结果是A∩B +/* +观察数据: +t_employee 看成A表 +t_department 看成B表 +此时t_employee (A表)中有 李红和周洲的did是NULL,没有对应部门, + t_department(B表)中有 测试部,在员工表中找不到对应记录的。 +*/ + +#查询所有员工的姓名,部门编号,部门名称 +#如果员工没有部门的,不要 +#如果部门没有员工的,不要 +/* +员工的姓名在t_employee (A表)中 +部门的编号,在t_employee (A表)和t_department(B表)都有 +部门名称在t_department(B表)中 +所以需要联合两个表一起查询。 +*/ +SELECT ename,did,dname +FROM t_employee INNER JOIN t_department; +#错误Column 'did' in field list is ambiguous +#因为did在两个表中都有,名字相同,它不知道取哪个表中字段了 +#有同学说,它俩都是部门编号,随便取一个不就可以吗? +#mysql不这么认为,有可能存在两个表都有did,但是did的意义不同的情况。 +#为了避免这种情况,需要在编写sql的时候,明确指出是用哪个表的did + +SELECT ename,t_department.did,dname +FROM t_employee INNER JOIN t_department; +#语法对,结果不太对 +#结果出现“笛卡尔积”现象, A表记录 * B表记录 +/* +(1)凡是联合查询的两个表,必须有“关联字段”, +关联字段是逻辑意义一样,数据类型一样,名字可以一样也可以不一样的两个字段。 +比如:t_employee (A表)中did和t_department(B表)中的did。 + +发现关联字段其实就是可以建外键的字段。当然联合查询不要求一定建外键。 + +(2)联合查询必须写关联条件,关联条件的个数 = n - 1. +n是联合查询的表的数量。 +如果2个表一起联合查询,关联条件数量是1, +如果3个表一起联合查询,关联条件数量是2, +如果4个表一起联合查询,关联条件数量是3, +。。。。 +否则就会出现笛卡尔积现象,这是应该避免的。 + +(3)关联条件可以用on子句编写,也可以写到where中。 +但是建议用on单独编写,这样呢,可读性更好。 + +每一个join后面都要加on子句 +A inner|left|right join B on 条件 +A inner|left|right join B on 条件 inner|left|right jon C on 条件 +*/ + + +SELECT ename,t_department.did,dname +FROM t_employee INNER JOIN t_department +ON t_employee.did = t_department.did; + +SELECT * +FROM t_employee INNER JOIN t_department +ON t_employee.did = t_department.did; + + +#查询部门编号为1的女员工的姓名、部门编号、部门名称、薪资等情况 +SELECT ename,gender,t_department.did,dname,salary +FROM t_employee INNER JOIN t_department +ON t_employee.did = t_department.did +WHERE t_department.did = 1 AND gender = '女'; + +#查询部门编号为1的员工姓名、部门编号、部门名称、薪资、职位编号、职位名称等情况 +SELECT ename,gender,t_department.did,dname,salary,job_id,jname +FROM t_employee INNER JOIN t_department ON t_employee.did = t_department.did + INNER JOIN t_job ON t_employee.`job_id` = t_job.`jid` +WHERE t_department.did = 1; + +``` + +2.左连接 + +```mysql +#演示左连接 +/* +(2)A +(3)A-A∩B +*/ +/* +观察数据: +t_employee 看成A表 +t_department 看成B表 +此时t_employee (A表)中有 李红和周洲的did是NULL,没有对应部门, + t_department(B表)中有 测试部,在员工表中找不到对应记录的。 +*/ +#查询所有员工,包括没有指定部门的员工,他们的姓名、薪资、部门编号、部门名称 +SELECT ename,salary,t_department.did,dname +FROM t_employee LEFT JOIN t_department +ON t_employee.did = t_department.did; +#查询的是A结果 A left join B + +#查询没有部门的员工信息 +SELECT ename,salary,t_department.did,dname +FROM t_employee LEFT JOIN t_department +ON t_employee.did = t_department.did +WHERE t_employee.did IS NULL; +#查询的结果是A - A∩B +#此时的where条件,建议写子表的关联字段is null,这样更准确一点。 +#如果要建外键,它们肯定有子表和父表的角色,写子表的关联字段is null +#因为父表中这个字段一般是主键,不会为null。 + +``` + +3.右链接 + +```mysql +/* +右连接 +(4)B +(5)B - A∩B +*/ +#演示右连接 +/* +观察数据: +t_employee 看成A表 +t_department 看成B表 +此时t_employee (A表)中有 李红和周洲的did是NULL,没有对应部门, + t_department(B表)中有 测试部,在员工表中找不到对应记录的。 +*/ +#查询所有部门,包括没有对应员工的部门,他们的姓名、薪资、部门编号、部门名称 +SELECT ename,salary,t_department.did,dname +FROM t_employee RIGHT JOIN t_department +ON t_employee.did = t_department.did; +#查询的是B结果 A RIGHT join B + +#查询没有员工部门的信息 +SELECT ename,salary,t_department.did,dname +FROM t_employee RIGHT JOIN t_department +ON t_employee.did = t_department.did +WHERE t_employee.did IS NULL; +#查询的结果是B - A∩B +#此时的where条件,建议写子表的关联字段is null,这样更准确一点。 +#如果要建外键,它们肯定有子表和父表的角色,写子表的关联字段is null +#因为父表中这个字段一般是主键,不会为null。 + + + +#查询所有员工,包括没有指定部门的员工,他们的姓名、薪资、部门编号、部门名称 +SELECT ename,salary,t_department.did,dname +FROM t_department RIGHT JOIN t_employee +ON t_employee.did = t_department.did; +#查询的是B结果 A RIGHT join B + +#查询没有部门的员工信息 +SELECT ename,salary,t_department.did,dname +FROM t_department RIGHT JOIN t_employee +ON t_employee.did = t_department.did +WHERE t_employee.did IS NULL; +#查询的结果是B - A∩B A right join B +#此时的where条件,建议写子表的关联字段is null,这样更准确一点。 +#如果要建外键,它们肯定有子表和父表的角色,写子表的关联字段is null +#因为父表中这个字段一般是主键,不会为null。 +``` + +自连接 + +```mysql +#演示特殊的联合查询/关联查询/多表查询:自连接 +#物理上,是一张表 +#逻辑上,是两张表 +/* +分析表结构:t_employee表 +mid:是表示存储员工的领导编号。即该员工归谁管。领导编号其实就是“领导”作为员工身份的员工编号 + 例如:eid为3的员工邓超远,他的mid是7,表示他的领导是员工编号为7的员工。 + eid为7的员工是贾宝玉,他的eid是7,贾宝玉作为员工来说,他的编号是7,作为领导来说,他的编号也是7。 + +mid的取值范围受到eid字段的限制。mid的值选择必须是eid现有值范围。 + +可以理解为mid和eid是关联字段,如果要建外键,可以在mid字段上建外键。 +foreign key(mid) references t_employee(eid) + +此时t_employee既是子表也是父表。 +员工表t_employee建立了外键: +CONSTRAINT `t_employee_ibfk_3` FOREIGN KEY (`mid`) REFERENCES `t_employee` (`eid`) ON DELETE SET NULL ON UPDATE CASCADE +*/ +#查询每一个员工自己的编号、名字、薪资和他的领导的编号、姓名、薪资。 +SELECT emp.eid,emp.ename,emp.salary, mgr.eid,mgr.ename,mgr.salary +FROM t_employee AS emp INNER JOIN t_employee AS mgr +ON emp.mid = mgr.eid; +#把t_employee当成两张表,通过取别名的方式 +#t_employee AS emp 把员工表 当成员工表 +# t_employee AS mgr 把员工表 当成存储领导信息的领导表 +#emp.mid = mgr.eid; 员工表的领导编号就是领导表的员工编号 +``` + +全连接 + +```mysql +/* +union实现 +(6)A∪B +(7)A∪B - A∩B + A-A∩B ∪ B-A∩B +*/ +#演示用union合并两个查询结果实现A∪B 和A∪B - A∩B +/* +union合并时要注意: +(1)两个表要查询的结果字段是一样的 +(2)UNION ALL表示直接合并结果,如果有重复的记录一并显示 + ALL去掉表示合并结果时,如果有重复记录,去掉。 +(3)要实现A∪B的结果,那么必须是合并 查询是A表结果和查询是B表结果的select语句。 +同样要实现A∪B - A∩B的结果,那么必须是合并查询是A-A∩B结果和查询是B-A∩B的select语句。 +*/ + +#查询所有员工和所有部门,包括没有指定部门的员工和没有分配员工的部门。 +SELECT * +FROM t_employee LEFT JOIN t_department +ON t_employee.did = t_department.did + +UNION + +SELECT * +FROM t_employee RIGHT JOIN t_department +ON t_employee.did = t_department.did; + + +#以下union会报错 +SELECT * FROM t_employee +UNION +SELECT * FROM t_department; +/* +错误代码: 1222 +The used SELECT statements have a different number of columns +两个Select语句的列数是不同的。 + +column:列,表中的字段。 +columns:很多的字段,即字段列表 +select 字段列表 from 表名称; +*/ + + +#联合 查询结果是A表的select 和查询结果是A∩B的select语句,是得不到A∪B +SELECT * +FROM t_employee LEFT JOIN t_department +ON t_employee.did = t_department.did + +UNION + +SELECT * +FROM t_employee INNER JOIN t_department +ON t_employee.did = t_department.did; + +#查询那些没有分配部门的员工和没有指定员工的部门,即A表和B表在对方那里找不到对应记录的数据。 +SELECT * +FROM t_employee LEFT JOIN t_department +ON t_employee.did = t_department.did +WHERE t_employee.did IS NULL + +UNION + +SELECT * +FROM t_employee RIGHT JOIN t_department +ON t_employee.did = t_department.did +WHERE t_employee.did IS NULL; +``` + diff --git "a/06 \351\231\210\345\277\227\344\274\237/\344\275\234\344\270\232.md" "b/06 \351\231\210\345\277\227\344\274\237/20230309 \345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" similarity index 72% rename from "06 \351\231\210\345\277\227\344\274\237/\344\275\234\344\270\232.md" rename to "06 \351\231\210\345\277\227\344\274\237/20230309 \345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" index da2a7a56e8e38ae1af60586a2155f1615aa286a7..d9e7c118d8d62acb23141ffbe2b06631fc34b171 100644 --- "a/06 \351\231\210\345\277\227\344\274\237/\344\275\234\344\270\232.md" +++ "b/06 \351\231\210\345\277\227\344\274\237/20230309 \345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" @@ -1,4 +1,19 @@ -``` mysql +# 子查询 + +## SELECT的SELECT中嵌套子查询 + +子查询:嵌套在另一个SQL语句中的查询。 +SELECT语句可以嵌套在另一个SELECT中,UPDATE,DELETE,INSERT,CREATE语句等。 + +## SELECT的WHERE或HAVING中嵌套子查询 + +当子查询结果作为外层另一个SQL的过滤条件,通常把子查询嵌入到WHERE或HAVING中。根据子查询结果的情况,分为如下三种情况。 + +- 当子查询的结果是单列单个值,那么可以直接使用比较运算符,如“<”、“<=”、“>”、“>=”、“=”、“!=”等与子查询结果进行比较。 +- 当子查询的结果是单列多个值,那么可以使用比较运算符IN或NOT IN进行比较。 +- 当子查询的结果是单列多个值,还可以使用比较运算符, 如“<”、“<=”、“>”、“>=”、“=”、“!=”等搭配ANY、SOME、ALL等关键字与查询结果进行比较。 + +```mysql create database student charset utf8; use student; create table stuinfo( @@ -69,12 +84,12 @@ select * from select stuno,stuname,stusex,max(score) zuigao from (select a.stuno,stuname,stusex,examNO from stuinfo a left join stuExam b on a.stuNO = b.stuNO) a left join stuMarks d on a.examNO=d.examNO group by stuID having zuigao > 80; -- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) - +select a.* from stuinfo a left join stumarks b on a.stuno=b.stuid where score > (select score from stumarks where stuid='s2501') group by stuid; -- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) - +select a.* from stuinfo a,(select stuid from stumarks where stuid!='s2501' group by stuid having max(score)>(select min(score) from stumarks where stuid='s2501')) b where a.stuno=b.stuid; -- 13.查询班上比所有男生年龄都要大的女生的信息 -select * from stuinfo where stusex='女' and stuage> (select max(stuage) from stuinfo where stusex='男'); +select * from stuinfo where stuage not in (select stuage from stuinfo where stusex='男') and stusex='女'; -- 14.查询出只要比某个男生年龄大的女生的信息 -select * from stuinfo where stusex='女' having stuage > any(select stuage from stuinfo where stusex='男'); +select * from stuinfo where stuage>(select min(stuage) from stuinfo where stusex='男') and stusex='女'; ``` diff --git "a/06 \351\231\210\345\277\227\344\274\237/20230310 \345\244\247\344\275\234\344\270\232.md" "b/06 \351\231\210\345\277\227\344\274\237/20230310 \345\244\247\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..06a38169192dc8e3f10945b4a8fcd364bbc117f7 --- /dev/null +++ "b/06 \351\231\210\345\277\227\344\274\237/20230310 \345\244\247\344\275\234\344\270\232.md" @@ -0,0 +1,169 @@ +``` mysql +create database wenju charset utf8; +use wenju; +create table shijian( + orderID int primary key, + orderDate datetime +); +create table wuping( + itemID int, + orderid int not null, + itemType varchar(11), + itemName varchar(11), + theNumber int, + theMoney int, + foreign key (orderid) references shijian (orderid) +); +insert into shijian values +(1,'2008-01-12 00:00:00.000'), +(2,'2008-02-10 00:00:00.000'), +(3,'2008-02-15 00:00:00.000'), +(4,'2008-03-10 00:00:00.000'); +insert into wuping values +(1,1,'文具','笔',72,2), +(2,1,'文具','尺',10,1), +(3,1,'体育用品','篮球',1,56), +(4,2,'文具','笔',36,2), +(5,2,'文具','固体胶',20,3), +(6,2,'日常用品','透明胶',2,1), +(7,2,'体育用品','羽毛球',20,3), +(8,3,'文具','订书机',20,3), +(9,3,'文具','订书针',10,3), +(10,3,'文具','裁纸刀',5,5), +(11,4,'文具','笔',20,2), +(12,4,'文具','信纸',50,1), +(13,4,'日常用品','毛巾',4,5), +(14,4,'日常用品','透明胶',30,1), +(15,4,'体育用品','羽毛球',20,3); +-- 根据图示,完成下列题目: +-- +-- 1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select itemid,orderdate,itemtype,itemname,thenumber,themoney from shijian a right join wuping b on a.orderid=b.orderid; +-- 2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select * from wuping where thenumber > 50; +-- 3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select itemid,orderdate,itemtype,itemname,thenumber,themoney,thenumber*themoney 总价 from shijian a right join wuping b on a.orderid=b.orderid; +-- 4.查询单价大于等于5 或者 数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select itemid,orderdate,itemtype,itemname,thenumber,themoney,thenumber*themoney 总价 from shijian a right join wuping b on a.orderid=b.orderid where themoney>=5 or thenumber>=50; +-- 5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select itemid,thenumber from wuping; +-- 6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- +-- 订单编号 产品类别 订购次数 总数量 +-- +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderid,itemtype,count(itemtype),sum(thenumber) from wuping group by orderid,itemtype; +``` + +```mysql +-- 1. 根据前面提供的表结构和表数据,创建数据库并分别创建这张四张表;并插入相关数据。(提醒:外键 +-- 请注意建表顺序和插入数据的顺序) (30分) +create database xiaoshuo charset utf8; +use xiaoshuo; +create table author( + author_id int primary key comment '作家编号', + author_name varchar(20) not null unique key comment '作家姓名', + credits int comment '积分', + vip_id varchar(20) not null comment '等级编号' +); +create table vip( + vip_id varchar(20) primary key comment '等级编号', + vip_name varchar(20) not null unique key comment '等级名称' +); +create table story( + story_id int primary key auto_increment comment '作品编号', + author_id int comment '作家编号', + type_id varchar(20) comment '类型编号', + story_name varchar(50) comment '作品名称', + views_number int comment'浏览器', + foreign key (author_id) references author(author_id), + foreign key (type_id) references type(type_id) +); +create table type( + type_id varchar(20) primary key comment '类型编号', + type_name varchar(20) not null unique key comment '类型名称' +); +insert into author values +(1001,'朱逸群',600,'VIP02'), +(1002,'范建',8510,'VIP04'), +(1003,'史珍香',981,'VIP02'), +(1004,'范统',2364,'VIP02'), +(1005,'杜子腾',257,'VIP01'), +(1006,'刘产',678,'VIP02'), +(1007,'杜琦燕',438,'VIP03'); +insert into vip VALUES +('VIP01','青铜作家'), +('VIP02','白银作家'), +('VIP03','黄金作家'), +('VIP04','钻石作家'); +insert into story values +(1,1002,'L03','母猪产后与护理师的二三事',6541), +(2,1005,'L04','拖拉机大战蜘蛛侠',563), +(3,1003,'L01','这只小龙虾不正经',8754), +(4,1006,'L04','一个爹爹三个娃',36354), +(5,1006,'L01','皇上滚开本宫只劫财',3674), +(6,1005,'L05','给长城贴瓷砖的小太监',6541), +(7,1003,'L03','不科学御兽',1257), +(8,1005,'L01','镜面管理局',3216), +(9,1004,'L02','关于我成为灭魂师之后',1147), +(10,1004,'L05','公子别秀',2078); +insert into type values +('L01','玄幻'), +('L02','奇幻'), +('L03','武侠'), +('L04','仙侠'), +('L05','都市'); +-- 2. 将story 表中的story_name字段类型改成varchar(40) 。(2分) +alter table story modify story_name varchar(40); +-- 3. 在author表中增加一个性别字段 字段名:author_sex,类型: char(10),要求默认值为'男'。 (3分) +alter table author add author_sex char(10) default '男'; +-- 4. 将作家编号为1005、1007的作家性别改为'女' 。(2分) +select * from author; +update author set author_sex='女' where author_id=1005 or author_id=1007; +-- 5. 作家杜子腾,写了一篇名为《拜登夸我很帅》的都市小说,有854个浏览量,请将这条信息插入到story +-- 表。(3分) +select * from story; +insert into story values (null,1005,'L05','拜登夸我很帅',854); +-- 6. 《拖拉机大战蜘蛛侠》这篇小说,浏览量涨了100,请更新story表中的相关数据。(2分) +update story set views_number=views_number+100 where story_name='拖拉机大战蜘蛛侠'; +-- 7. 请删除story表的中《皇上滚开本宫只劫财》这篇小说相关数据。(2分) +delete from story where story_name='皇上滚开本宫只劫财'; +-- 8. 查询 浏览量大于 8000的小说的作者编号和小说作品名称。(2分) +select * from story where views_number>8000; +-- 9. 查询积分大于1000 并且会员等级高于vip03的作家所有信息。(3分) +select * from author where credits>1000 and vip_id>'vip03'; +-- 10. 查询姓名以杜字开头的作家的姓名,积分和等级编号。(3分) +select * from author where author_name like '杜%'; +-- 11. 查询积分在100、1000之间的作家信息,以积分降序排列。 (3分) +select * from author where credits > 100 and credits < 1000 order by credits desc; +-- 12. 查询出小说的总浏览量,最高浏览量,最小浏览量,平均浏览量,给字段用上中文别名。(3分) +select sum(views_number) 总浏览量,max(views_number) 最高浏览量,min(views_number) 最小浏览量,avg(views_number) 平均浏览量 from story; +-- 13. 查询各种等级的作家的平均积分和作家数量,并对查询结果使用中文别名。(3分) +select round(avg(credits),2) 平均积分,count(vip_id) 作家数量 from author group by vip_id; +-- 14. 查询小说数量大于等于2的分类编号和小说数量。(4分) +select author_id,count(author_id) a from story group by author_id having a>=2; +-- 15. 查询所有小说中浏览量最少的书的作品编号、作品名称和类型编号、浏览量。(4分) +select story_id,story_name,type_id,views_number from story where views_number=(select min(views_number) from story); +-- 16. 查询积分比刘产高的作者所有信息。(5分) +select * from author where credits>(select credits from author where author_name='刘产'); +-- 17. 查询出哪些白银作家是没有写小说的,显示这些作家的姓名、等级名称。(8分) +select author_name,vip_id from story a right join (select * from author where vip_id=(select vip_id from vip where vip_name='白银作家')) b on a.author_id=b.author_id where story_name is null; +-- 18. 找出写过作品浏览量大于5000的作家的所有作品中浏览量不到1000的作品信息(8分) +select author_id from story where views_number>5000; +select * from story where author_id = any(select author_id from story where views_number>5000) and views_number <1000; +-- 19. 查询所有小说的小说编号、小说名称、浏览量、分类名称、作者姓名、作者积分、作者等级名称,结果 +-- 字段要用中文别名,并按浏览量降序排列,如果浏览量一样的,再按积分降序排列。(10分) +select story_id 小说编号,story_name 小说名称,views_number 浏览量,type_name 分类名称,author_name 作者姓名,credits 作者积分,vip_name 作者等级名称 from vip e right join +(select story_id,story_name,views_number,type_name,author_name,credits,vip_id from author c right join +(select story_id,story_name,views_number,type_name,author_id from story a left join type b on a.type_id=b.type_id) d on c.author_id=d.author_id) f on e.vip_id=f.vip_id order by 浏览量,作者积分; + +``` + diff --git "a/06 \351\231\210\345\277\227\344\274\237/20230313 \344\275\234\344\270\232.md" "b/06 \351\231\210\345\277\227\344\274\237/20230313 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..3f44e58a9bae6c3e840147683dda64c434e840c8 --- /dev/null +++ "b/06 \351\231\210\345\277\227\344\274\237/20230313 \344\275\234\344\270\232.md" @@ -0,0 +1,85 @@ +```mysql +create database studen charset utf8; +use studen; +create table stuinfo( + stuNO varchar(11) primary key, + stuName varchar(11) unique key not null, + stuAge int not null, + stuAddress varchar(11) not null, + stuSeat int not null, + stuSex int not null +); +create table stuexam( + examNO int not null unique key auto_increment, + stuNO varchar(11) primary key, + writtenExam int not null, + labExam int not null, + foreign key (stuNO) references stuinfo(stuNO) +); +insert into stuinfo values +('s2501','张秋利',20,'美国硅谷',1,1), +('s2502','李斯文',18,'湖北武汉',2,0), +('s2503','马文才',22,'湖南长沙',3,1), +('s2504','欧阳俊雄',21,'湖北武汉',4,0), +('s2505','梅超风',20,'湖北武汉',5,1), +('s2506','陈旋风',19,'美国硅谷',6,1), +('s2507','陈风',20,'美国硅谷',7,0); +insert into stuexam values +(1,'s2501',50,70), +(2,'s2502',60,65), +(3,'s2503',86,85), +(4,'s2504',40,80), +(5,'s2505',70,90), +(6,'s2506',85,90); +-- 1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuNO 学号,stuName 姓名,stuAge 年龄,stuAddress 地址,stuSeat 号数,stuSex 性别 from stuinfo; +-- 2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName 姓名,stuAge 年龄,stuAddress 地址 from stuinfo; +-- 3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +select stuNO 学号,writtenExam 笔试,labExam 机试 from stuexam; +-- 5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分这四列的信息 +select stuNO 学号,writtenExam 笔试,labExam 机试,writtenExam+labExam 总分 from stuexam; +-- 6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select distinct stuAddress from stuinfo; +-- 7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名 +select stuAge 年龄 from stuinfo group by stuAge; +-- 8.查询学生信息表(stuInfo)中前3行记录 +select * from stuinfo limit 0,3; +-- 9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select stuName,stuSeat from stuinfo limit 0,4; +-- 11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from stuinfo where stuAddress='湖北武汉' and stuAge=20; +-- 12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from stuexam where labExam between 60 and 80 order by labExam desc; +-- 13.查询来自湖北武汉或者湖南长沙的学生的所有信息 +select * from stuinfo where stuAddress='湖北武汉' or stuAddress='湖南长沙'; +-- 14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from stuexam where writtenExam not between 70 and 90 order by writtenExam asc; +-- 15.查询年龄没有写的学生所有信息 +select * from stuinfo where stuAge is null; +-- 16.查询年龄写了的学生所有信息 +select * from stuinfo where stuage is not null; +-- 17.查询姓张的学生信息 +select * from stuinfo where stuName like '张%'; +-- 18.查询学生地址中有‘湖’字的信息 +select * from stuinfo where stuAddress like '%湖%'; +-- 19.查询姓张但名为一个字的学生信息 +select * from stuinfo where stuName like '张_'; +-- 20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from stuinfo where stuname like '__俊%'; +-- 21.按学生的年龄降序显示所有学生信息 +select * from stuinfo order by stuAge desc; +-- 22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from stuinfo order by stuAge desc,stuSeat asc; +-- 23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select examNO,a.stuNO,writtenExam,labExam from stuinfo a right join stuexam b on a.stuno=b.stuNO where writtenExam=(select max(writtenExam) from stuexam); +-- 24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select examNO,a.stuNO,writtenExam,labExam from stuinfo a right join stuexam b on a.stuno=b.stuNO where labExam=(select min(labExam) from stuexam); +-- 25.查询每个地方的学生的平均年龄 +select stuAddress,round(avg(stuAge),2) 平均年龄 from stuinfo group by stuAddress; +-- 26.查询男女生的分别的年龄总和 +select stuSex,sum(stuAge) 年龄总和 from stuinfo group by stuSex; +-- 27.查询每个地方的男女生的平均年龄和年龄的总和 +select stuSex,round(avg(stuAge),2) 平均年龄,sum(stuAge) 年龄总和 from stuinfo group by stuSex; +``` +