Mysql查询小作业
数据准备
drop table if exists class;
create table class(
class_no int(2) unsigned zerofill primary key auto_increment comment '班级编号',
class_name varchar(30) not null comment '班级名称'
);
insert into class values(1, '培优班');
insert into class values(2, '普通班');
drop table if exists student;
create table student(
stu_no int(2) unsigned zerofill primary key auto_increment comment '学员编号',
stu_name varchar(30) not null comment '学员姓名',
stu_sex varchar(3) not null comment '学员性别',
stu_age tinyint(2) unsigned zerofill comment '学员年代',
class_no int(2) unsigned zerofill comment '所在班级编号',
foreign key(class_no) references class(class_no)
);
insert into student values(01, '李白', '男', 18, 01);
insert into student values(02, '杜甫', '男', 20, 01);
insert into student values(03, '张飞', '男', 32, 02);
insert into student values(04, '韩信', '男', 26, 02);
insert into student values(05, '了龙', '男', 27, 02);
insert into student values(06, '大乔', '女', 17, 01);
insert into student values(07, '小乔', '女', 16, 01);
insert into student values(08, '小乔', '女', 16, 01);
insert into student values(09, '关哥', '男', 32, 02);
insert into student values(10, '刘备', '男', 36, null);
alter table student drop foreign key `student_ibfk_1`;
1: 查询出student表中年龄最大的学生
select * from student where stu_age = (select max(stu_age) from student);
2: 查询出student表中年龄最小的学生
select * from student where stu_age = (select min(stu_age) from student);
3: 查询出02号班中最大的年龄是多少
select max(stu_age) from student where class_no = 2;
4: 查询出01号班中最小的年龄是多少
select min(stu_age) from student where class_no = 1;
5: 查询出01号班中有多少个学生
select count(*) from student where class_no = 1;
6: 查询出01号班中平均年龄是多少
select avg(stu_age) from student where class_no = 1;
7:查询出没有班级的学生
(失败)select * from student where class_no not in ( select class_no from class);(查询出结果为空?)
(成功)select * from student where not exists ( select distinct(class_no) from class where student.class_no = class.class_no); // null值的特殊性,不能使用not in来查询,NULL值在与任意值比较时总是假的(FALSE),并且包含NULL的一个表达式总是产生一个NULL值
8: 查询出02号班级中年龄大于30岁的男学员
select * from student where class_no = 2 and stu_sex = '男' and stu_age > 30;
9: 查询出所有的女学员姓名,并在名字后加上‘大美女’名字
select concat(stu_name,'大美女') from student where stu_sex = '女';
10: 查询出1号班级的所有男学员 还有 没有班级的学员
select * from student where (class_no = 1 || class_no is null) and stu_sex = '男';
select * from student where class_no = 1 and stu_sex = '男' || not exists ( select class_no from class where student.class_no = class.class_no);
11: 查询出年龄在20-30岁的学员
select * from student where stu_age between 20 and 30;
12: 查询出所有名字中第二个字符是‘乔’的学员的平均工资(没有工资列,改为平均年龄或者添加工资列)
select avg(stu_age) from student where stu_name like('_乔%');
13: 查询出班中所有学生的姓名,其中不包括重复的
select distinct(stu_name) from student where class_no is not null;
14: 查询姓名,性别两个字段, 不包括重复的记录
select distinct stu_name,stu_age from student;
15: 查询出1号部门中所有的学员,根据年龄升序排序,年龄相同的则按照学号降序排序
select * from student where class_no = 1 order by stu_age asc,stu_no desc;
16: 查询出最后一条数据
(失败)select * from student limit (select count(stu_no)-1 from student),1;// 参数不能为表达式
select * from student order by stu_no desc limit 1;
17: 查询出学号为6的后面的3条数据
select * from student where stu_no > 6 limit 3;
18: 查询出学号为6的前面的3条数据
select * from student where stu_no < 6 order by stu_no desc limit 3;
19: 删除最后一条记录,(在不知道最后一条记录id的情况)
delete from student where stu_no in (select stu_no from (select stu_no from student order by stu_no desc limit 1) as temtable);// MySQL不能指定更新的目标表在FROM子句,所以先将删除的数据放到临时表中再进行删除
delete from student order by stu_no desc limit 1;
20: 删除掉学号为6的后面的3条数据(同理(19))
delete from student where stu_no in (select stu_no from (select stu_no from student where stu_no > 6 limit 3) as temtable);
delete from student where stu_no > 6 limit 3;
Mysql查询小作业的更多相关文章
- mysql之连接查询小作业
#数据准备drop table if exists class;create table class( class_no int(2) unsigned zerofill primary key ...
- mysql 查询小技巧
数据字段中存放的是id集,形如 1,2,15,35 也可类推json格式 查询时不用拆分了, 用上 instr.concat搜索和连接字符串 查询fids中包含15的 select * from ...
- mysql 查询小demo
两张表的的结构如下,需求是写出从one表到two表和从two表到one表的查询转换. create table student_one( name varchar(50) default '' not ...
- mysql查询小技巧
如果所传bookTypeName为空则执行select * from t_bookType(搜索框里未输入信息) 否则追加 and bookTypeName like '%"+bookTy ...
- MySQL 表记录查询小练习
表记录查询小练习 查看岗位是teacher的员工姓名.年龄 查看岗位是teacher且年龄大于26岁的员工姓名.年龄 查看岗位是teacher且薪资在12000-16000范围内的员工姓名.年龄.薪资 ...
- 《MySQL面试小抄》查询缓存机制终面
<MySQL面试小抄>查询缓存机制终面 我是肥哥,一名不专业的面试官! 我是囧囧,一名积极找工作的小菜鸟! 囧囧表示:小白面试最怕的就是面试官问的知识点太笼统,自己无法快速定位到关键问题点 ...
- mysql查询常用小语句
mysql 查询某个库里表的数量 在mysql中有个数据库information_schema下的表tables记录了所有数据库中所有的表相关信息 TABLE_SCHEMA 数据库名称 SELECT ...
- 数据库MySQL(课下作业,必做)
数据库MySQL(课下作业,必做) 题目要求: 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入 ...
- mysql查询性能优化
mysql查询过程: 客户端发送查询请求. 服务器检查查询缓存,如果命中缓存,则返回结果,否则,继续执行. 服务器进行sql解析,预处理,再由优化器生成执行计划. Mysql调用存储引擎API执行优化 ...
随机推荐
- python元类理解2
恩,对元类理解又有新的收获,其实类似于装饰器,只不过装饰器是修饰函数,元类用来定制一个类. 代码如下,这是一个使用了函数做元类传递给类: input: def upper_attr(class_nam ...
- DirectSound---捕获音频、Qml/C++ 集成交互
DirectSound的音频捕获原理和播放原理差不多,内部在一个缓冲区上循环写入捕获到的数据,并且提供notify通知功能. 1. 音频捕获 因为捕获流程和播放流程类似,我们就不在这里赘述了,只给出简 ...
- python全栈学习--day4
列表 说明:列表是python中的基础数据类型之一,它是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: 1 li = ['alex',123,Ture,(1,2,3,'wu ...
- spring学习笔记二 注解及AOP
本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...
- eclipse如何debug调试jdk源码(任何源码)并显示局部变量
最近要看struts2源码 仿照了一下查看jdk源码的方式 首先你要有strtus2的jar包和源码,在struts官网上下载时,选择full版本,里面会有src也就是源码了. jar导入项目,保证可 ...
- 记录python接口自动化测试--把操作excel文件的方法封装起来(第五目)
前面补充了如何来操作excel文件,这次把如何获取excel文件的sheet对象.行数.单元格数据的方法进行封装,方便后面调用 handle_excel.py# coding:utf-8 import ...
- TensorFlow实现Softmax Regression识别手写数字中"TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败”问题
出现问题: 在使用TensorFlow实现MNIST手写数字识别时,出现"TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应 ...
- 冲刺NO.5
Alpha冲刺第五天 站立式会议 项目进展 今日项目完成内容主要包括了JS的学习,事务管理员模块与学生模块的完善与补充,并且开始编写信用信息管理模块和奖惩事务管理模块. 问题困难 前端部分的技术掌握的 ...
- 设计模式NO.2
设计模式NO.2 本次博客内容为第二次设计模式的练习.根据老师的要求完成下列题目: 题目1 如果需要开发一个跨平台视频播放器,可以在不同操作系统平台(如Windows.Linux.UNIX等)上播放多 ...
- faster-rcnn 结构杂谈
faster-rcnn结构图: (只截取了最难理解的部分) 这个网络看似很复杂,但是理解了其中关键的层,就基本可以掌握这个结构了.要看源码!!要看源码!!要看源码 !!重要的事情说三遍. 关键的层: ...