【作业】2022.5.24 MySQL 查操作

本文详细介绍了SQL查询的基本语法和高级用法,包括投影、筛选、排序、聚合、分组、连接、子查询等操作,涵盖了从简单查询到复杂数据分析的多个方面,旨在帮助读者掌握数据库查询的核心技能。
use school;

-- 查询所有学生的所有信息
select * from tb_student;

-- 查询学生的学号、姓名和籍贯(投影)
select stu_id, stu_name, stu_addr from tb_student;

-- 查询所有课程的名称及学分(投影和别名)
select cou_name as 课程名称, cou_credit as 课程学分 from tb_course;

-- 查询所有女学生的姓名和出生日期(筛选)
select stu_name, stu_birth from tb_student where stu_sex=0;

-- 查询籍贯为“四川成都”的女学生的姓名和出生日期(筛选)
select stu_name, stu_birth from tb_student where stu_sex=0 and stu_addr='四川成都';

-- 查询籍贯为“四川成都”或者性别是女的学生
select * from tb_student where stu_sex=0 or stu_addr='四川成都';

-- 查询所有80后学生的姓名、性别和出生日期(筛选)
select stu_name, stu_sex, stu_birth from tb_student where stu_birth>='1980-01-01';

-- 查询学分大于2的课程的名称和学分(筛选)
select cou_name, cou_credit from tb_course where cou_credit>2;

-- 查询学分是奇数的课程的名称和学分(筛选)
select cou_name, cou_credit from tb_course where cou_credit % 2 = 1;

-- 查询选择选了1111的课程考试成绩在90分以上的学生学号(筛选)
select stu_id from tb_record where cou_id = 1111 and score > 90;

-- 查询名字叫“杨过”的学生的姓名和性别
select stu_name, stu_sex from tb_student where stu_name = '杨过';

-- 查询姓“杨”的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨%';

-- 查询姓“杨”名字两个字的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨_';

-- 查询姓“杨”名字三个字的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨__';

-- 查询名字中有“不”字或“嫣”字的学生的姓名(模糊)
select stu_name from tb_student 
where stu_name like '%不%' or stu_name like '%嫣%';

-- 查询姓“杨”或姓“林”名字三个字的学生的姓名(正则表达式模糊查询)
select stu_name from tb_student where regexp_like (stu_name, '[杨林]..');

-- 查询没有录入籍贯的学生姓名(空值处理)
select stu_name from tb_student where stu_addr is null;

-- 查询录入了籍贯的学生姓名(空值处理)
select stu_name from tb_student where stu_addr is not null;

-- 查询学生选课的所有日期(去重)
select distinct sel_date from tb_record;

-- 查询学生的籍贯(去重)
select distinct stu_addr from tb_student;

-- 查询男学生的姓名和生日按年龄从大到小排列(排序)
select stu_name, stu_birth from tb_student 
where stu_sex = 1 
order by stu_birth;

-- 补充:将上面的生日换算成年龄(日期函数、数值函数)
select stu_name, timestampdiff(year, stu_birth, curdate()) from tb_student
where stu_sex = 1
order by stu_birth;

-- 查询年龄最大的学生的出生日期(聚合函数)
select min(stu_birth) from tb_student;

-- 查询年龄最小的学生的出生日期(聚合函数)
select max(stu_birth) from tb_student;

-- 查询编号为1111的课程考试成绩的最高分(聚合函数)
select max(score) from tb_record where cou_id = 1111;

-- 查询学号为1001的学生考试成绩的最低分(聚合函数)
select min(score) from tb_record where stu_id = 1001;

-- 查询学号为1001的学生考试成绩的平均分(聚合函数)
select avg(score) from tb_record where stu_id = 1001;

-- 查询学号为1001的学生考试成绩的平均分,如果有null值,null值算0分(聚合函数)
select avg(ifnull(score, 0)) from tb_record where stu_id = 1001;

-- 查询学号为1001的学生考试成绩的标准差(聚合函数)
select stddev(score) from tb_record where stu_id = 1001;

-- 查询男女学生的人数(分组和聚合函数)
select stu_sex as 性别, count(stu_sex) as 人数 
from tb_student group by stu_sex;

-- 查询每个学院学生人数(分组和聚合函数)
select col_id, count(col_id) 
from tb_student group by col_id;

-- 查询每个学院男女学生人数(分组和聚合函数)
select col_id, stu_sex, count(stu_sex) 
from tb_student group by col_id, stu_sex;

-- 查询每个学生的学号和平均成绩(分组和聚合函数)
select stu_id, avg(score) from tb_record group by stu_id;

-- 查询平均成绩大于等于90分的学生的学号和平均成绩
select stu_id, avg(score) from tb_record group by stu_id having avg(score) > 90;

-- 查询1111、2222、3333三门课程平均成绩大于等于90分的学生的学号和平均成绩
select stu_id, avg(score) from tb_record where cou_id in (1111, 2222, 3333)
group by stu_id having avg(score) > 90 and count(stu_id) = 3;

-- 查询年龄最大的学生的姓名(子查询/嵌套查询)
select stu_name from tb_student where stu_birth in 
(select min(stu_birth) from tb_student);

-- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算)
select stu_name from tb_student where stu_id in
(select stu_id from tb_record group by stu_id having count(stu_id) > 2);

-- 查询学生的姓名、生日和所在学院名称
select stu_name, stu_birth, col_name from tb_college 
join tb_student on tb_student.col_id=tb_college.col_id; 

-- 查询学生姓名、课程名称以及成绩(连接查询/联结查询)
select stu_name, cou_name, score from tb_record
join (tb_student, tb_course) on tb_student.stu_id=tb_record.stu_id and tb_course.cou_id=tb_record.cou_id;

-- 补充:上面的查询结果取前5条数据(分页查询)
select stu_name, cou_name, score from tb_record
join (tb_student, tb_course) on tb_student.stu_id=tb_record.stu_id and tb_course.cou_id=tb_record.cou_id
limit 5 offset 0;

-- 补充:上面的查询结果取第6-10条数据(分页查询)
select stu_name, cou_name, score from tb_record
join (tb_student, tb_course) on tb_student.stu_id=tb_record.stu_id and tb_course.cou_id=tb_record.cou_id
limit 5 offset 5;

-- 补充:上面的查询结果取第11-15条数据(分页查询)
select stu_name, cou_name, score from tb_record
join (tb_student, tb_course) on tb_student.stu_id=tb_record.stu_id and tb_course.cou_id=tb_record.cou_id
limit 5 offset 10;

-- 查询选课学生的姓名和平均成绩(子查询和连接查询)
select stu_name, avg(score) from tb_record 
join tb_student on tb_student.stu_id = tb_record.stu_id
group by tb_record.stu_id;

-- 查询学生的姓名和选课的数量
select stu_name, count(tb_record.stu_id) from tb_record 
join tb_student on tb_student.stu_id = tb_record.stu_id
group by tb_record.stu_id;

-- 查询每个学生的姓名和选课数量(左外连接和子查询)
 select stu_name, count(tb_record.stu_id) from tb_record 
join tb_student on tb_student.stu_id = tb_record.stu_id
group by tb_record.stu_id;

-- 补充练习
-- 01. 查询课程1111比课程2222成绩高的所有学生的学号

-- 02. 查询没有学过张三丰老师的课程的同学的学号和姓名
select stu_id, stu_name from tb_student where stu_id not in(
select tb_student.stu_id from tb_student 
join (tb_teacher, tb_record, tb_course) on 
tb_teacher.tea_id = tb_course.tea_id and 
tb_student.stu_id = tb_record.stu_id and
tb_course.cou_id = tb_record.cou_id
where tea_name = '张三丰');
<think>好的,我现在需要解决用户在Vue中组件未正确更新的问题,尤其是在点击“复制”按钮后弹窗内容没有及时显示,而是延迟加载的情况。用户提到希望通过修改key值或其他方法强制重渲染子组件。让我先理清楚可能的原因和解决方案。 首先,根据用户提供的引用内容,特别是引用[1]中的例子,他们之前通过设置key为时间戳来强制子组件刷新。这似乎是一个有效的方法,因为Vue的响应式系统在key改变时会重新创建组件实例。不过用户现在遇到了类似的问题,可能需要同样的方法,但需要确认是否正确应用。 然后,引用[2]提到Vue的异步更新队列,可能导致状态变化不会立即触发DOM更新。如果在同一个事件循环中多次修改状态,Vue可能只会处理最后一次的变更。这可能意味着用户在点击“复制”按钮后,某些状态没有被及时更新,导致弹窗内容没有正确显示。 接下来,我需要考虑可能的解决方案。除了修改key值之外,还有其他方法可以强制组件重新渲染,比如使用this.$forceUpdate()或者改变组件的某个prop。但修改key值通常是推荐的方法,因为它更直接地触发组件的销毁和重新创建,确保状态完全重置。 用户的问题具体是在点击“复制”按钮时弹窗内容没有正确显示。可能的情况是,弹窗组件依赖于某些数据,而这些数据在按钮点击后没有及时触发更新。例如,可能在打开弹窗时,数据还未被正确赋值,或者由于Vue的异步更新机制,DOM没有及时渲染。 我需要检查用户可能的代码结构。假设弹窗组件是一个子组件,当点击“复制”按钮时,父组件会修改某些数据,然后显示弹窗。如果弹窗的内容依赖于这些数据,但数据的变化没有被及时检测到,或者弹窗组件没有重新渲染,就会导致内容显示不正确。 根据引用[1]中的例子,用户之前通过给子组件设置:key="timer",并在打开弹窗时更新timer的值,从而强制子组件重新渲染。同样的方法可能适用于当前的问题。即在点击“复制”按钮时,更新key的值,确保子组件每次打开时都重新创建,而不是复用之前的实例。 另外,需要注意在关闭弹窗时,是否正确地重置了相关的数据和key的值。例如,在弹窗关闭事件中,将timer置为null或新的时间戳,以确保下次打开时key值不同,触发重新渲染。 还有可能涉及到Vue的响应式数据更新问题。如果用户的数据结构较为复杂,或者使用了嵌套对象,Vue可能无法检测到数据的变化。这时候可能需要使用Vue.set或this.$set来确保属性的响应式更新。 此外,引用[3]中提到的主进程和渲染进程之间的通信机制,可能不直接相关,但如果用户的应用涉及Electron或其他类似框架,可能需要考虑跨进程通信对状态更新的影响,不过用户当前的问题集中在Vue组件本身,暂时可以排除这方面的因素。 总结可能的解决步骤: 1. 在弹窗子组件上使用:key属性,并将其绑定到一个响应式的变量(如timer)。 2. 在点击“复制”按钮时,更新timer的值,通常使用时间戳,确保每次点击时key值不同。 3. 在弹窗关闭时,重置timer或其他相关状态,确保下次打开时能再次触发更新。 4. 检查数据更新的时机,确保在打开弹窗前数据已经正确赋值,可能需要使用this.$nextTick来确保DOM更新。 需要验证用户是否正确地应用了这些步骤。例如,是否在点击按钮时更新了key的值,是否在关闭时重置,是否有其他因素导致数据未及时更新。 另外,如果用户的数据更新存在异步操作,比如从API获取数据,可能需要确保在数据加载完成后再打开弹窗,或者使用v-if指令控制弹窗的显示,结合key的变化来强制重渲染。 最后,还要考虑是否有其他优化手段,比如使用计算属性或侦听器来响应数据变化,但关键点还是通过修改key来强制组件重新渲染,确保内容正确显示。</think>在Vue中强制子组件重渲染的解决方案如下: ### 一、通过修改key值强制重渲染 1. **绑定动态key属性** 在弹窗子组件上绑定`:key`属性,值为响应式变量: ```vue <codemirror-editor ref="editor" :key="componentKey" v-model="content" ></codemirror-editor> ``` 2. **点击操作时更新key值** 在"复制"按钮事件中修改key值触发重建: ```javascript handleCopy() { this.componentKey = new Date().getTime(); // 时间戳更新key this.showModal = true; // 显示弹窗 } ``` 3. **关闭弹窗时重置状态** 弹窗关闭时清空关键数据: ```javascript closeModal() { this.componentKey = null; // 可选重置操作 this.content = ''; this.showModal = false; } ``` ### 二、其他辅助优化方法 1. **使用$nextTick确保DOM更新** 在异步操作后强制更新: ```javascript this.$nextTick(() => { this.componentKey = new Date().getTime(); }); ``` 2. **通过v-if控制生命周期** 结合`v-if`与`key`双重保障: ```vue <codemirror-editor v-if="showModal" :key="componentKey" ></codemirror-editor> ``` 3. **使用Vue.set处理深层次对象** 当数据层级较深时: ```javascript Vue.set(this.dataObj, 'nestedProp', newValue); ``` ### 三、原理说明 Vue通过虚拟DOM的diff算法判断组件是否需要更新。当`key`值改变时,会销毁旧组件实例并创建新实例,达到强制重渲染的目的[^1]。这种方法比`$forceUpdate()`更彻底,后者仅触发重新渲染但不重置组件状态[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sprite.Nym

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值