MySQL复习2

news2024/9/22 1:01:32

高级查询

准备

create database greatselect;
use greatselect;

drop table if exists `class`;
create table `class` (
    `cid` int(11) not null auto_increment,
    `caption` varchar(32) not null,
    primary key (`cid`)
)engine = innoDB AUTO_INCREMENT=5 default charset = utf8;

create table if not exists `teacher` (
    `tid` int(11) not null auto_increment,
    `tname` varchar(32) not null ,
    primary key (`tid`)
) engine = innoDB auto_increment=6 default charset = utf8;

drop table if exists `course`;
create table `course` (
    `cid` int(11) not null auto_increment,
    `cname` varchar(32) not null,
    `teacher_id` int(11) not null,
    primary key (`cid`),
    key `fk_course_teacher` (`teacher_id`),
    constraint `fk_course_teacher` foreign key (`teacher_id`) references `teacher` (`tid`)
)engine = innoDB auto_increment=5 default charset = utf8;

drop table if exists `student`;
create table `student` (
    `sid` int(11) not null auto_increment,
    `gender` char(1) not null ,
    `class_id` int(11) not null ,
    `sname` varchar(32) not null ,
    primary key (`sid`),
    key `fk_class` (`class_id`),
    constraint `fk_class` foreign key (`class_id`) references `class` (`cid`)
)engine = innoDB auto_increment=17 default charset = utf8;

drop table if exists `scocre`;
create table `score` (
    `sid` int(11) not null auto_increment,
    `student_id` int(11) not null ,
    `course_id` int(11) not null ,
    `num` int (11) not null ,
    primary key (`sid`),
    key `fk_score_student` (`student_id`),
    key `fk_course_id` (`course_id`),
    constraint `fk_score_student` foreign key (`student_id`) references `student` (`sid`),
    constraint `fk_score_course` foreign key (`course_id`) references `course` (`cid`)
)engine = innoDB auto_increment=53 default charset = utf8;

show tables;

基础查询

-- 全部查询
select * from student;
-- 只查询部分字段
select `sname`, `class_id` from student;
-- 别名 列名 不要用关键字
select `sname` as ‘姓名’, `class_id` as '班级ID' from student;
-- 把查询出来的结果的重复记录去掉
select distinct `class_id` from student;

条件查询

-- 查询姓名为lennlouis的学生信息
select * from `student` where `name` = 'lennlouis';
-- 查询性别为 男,且班级为 2 的学生信息
select * from `student` where `gender` = '男' and `class_id` = 2;

范围查询

-- 查询班级id 1 到 3 的学生信息
select * from `student` where `class_id` between 1 and 3;

判空查询

-- is null 判断造成索引失效
select * from `student` where `class_id` is not null;
select * from `student` where `class_id` is null;

-- 字符串不为空
select * from `student` where `gender` <> '';
-- 字符串为空
select * from `student` where `gender` = '';

模糊查询

-- 使用 like关键字,“%”代表任意数量的字符,“_”代表占位符
-- 查询名字为 m开头的学生信息
select * from `teacher` where `tname` like 'l%';
-- 查询姓名里第二个字为 ‘e’的小学生的信息
select * from `teacher` where `tname` like '_e%';

分页查询

-- 分页查询主要用于查看第 N条 到第 M条的信息,通常和排序查询一起使用
-- 使用limit关键字,第一个参数表示从条记录开始显示,第二个参数表示要显示的数目。表中默认第一条记录的参数为0
-- 查询第二条到第三条内容
select * from `student` limit 1, 2;

查询后排序

-- 关键字:order by field, asc:升序, desc:降序
select * from `score` order by `num` asc;
\-- 多个字段排序
select * from `score` order by `course_id` desc, `num` desc

聚合查询

聚合函数描述
sum ()计算某一列的总和
avg ()计算某一列的平均值
max ()某一列的最大值
min ()某一列的最小值
count ()某一列的行数
select sum(`num`) from `score`;
select avg(`num`) from `score`;
select max(`num`) from `score`;
select min(`num`) from `score`;
select count(`num`) from `score`;

分组查询

-- 分组加 group_concat
select `gender`, group_concat(`age`) as ages from `student` group by `gender`;
-- 可以把查询出来的结果根据某个条件来分组显示
select `gender` from `student` group by `gender`;
-- 分组加聚合
select `gender`, count(*) as num from `student` group by `gender`;
-- 分组条件
select `gender`, count(*) as num from `student` group by `gender` having num > 6;

联表查询

image.png

INNER JOIN

取两张表有对应关系的记录

select
	cid
from
	`course`
inner join `teacher` on course.teacher_id = teacher.tid;

LEFT JOIN

在内联的基础上保留左边表上没有对应关系的记录

select 
	course.cid
from
	`course`
left join `teacher` on course.teacher_id = teacher.tid;

RIGHT JOIN

在内联的基础上保留右边表上没有对应关系的记录

select
	course.cid
from
	`course`
right join `teacher` on course.teacher_id = teacher.tid;

子查询/合并查询

单行子查询

select * from course where course.teacher_id = (select tid from teacher where tname = 'wood');

多行子查询

多行子查询返回多行记录的子查询

  • IN 关键字:运算符可以检测结果集中是否存在特定的值,如果检测成功就执行外部的查询。
  • EXISTS 关键字:内层查询语句不返回查询记录。而是返回一个真假值。如果内层查询语句查询到满足条件的记录,就返回一个真值(true),否则,将返回一个假值(false)。当返回的值为 true 时,外层查询语句将进行查询;当返回的值为 false 时,外层查询语句不进行查询或查询不出任何记录。
  • ALL 关键字:表示满足所有条件。使用 ALL 关键字时,只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。
  • ANY 关键字:允许创建一个表达式,对子查询的返回值列表,进行比较,只要满足内层子查询条件中的任意一个比较条件,就返回一个结果作为外层查询条件。
  • 在 FROM 子句中使用子查询:子查询出现在 from 子句中,这种情况下将子查询当做一个临时表使用。
select * from student where class_id in (select cid from course where teacher_id = 2)

select * from student where exists(select cid from course where cid = 5);

select student_id, sname from (select * from score where course_id = 1 or course_id = 2) as A left join student on A.student_id = student.sid;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2098880.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

报考条件、材料、流程?关于CISP认证,你必须要了解这些

信息安全一直是一个火热的话题&#xff0c;在近两年又被推上了高峰。对此&#xff0c;相关认证也不例外。 很多朋友都想get一本安全方向的证书&#xff0c;在广大安全方向的认证中&#xff0c;CISP可谓是发展迅猛&#xff0c;并越来越广为人知&#xff0c;也越来越受到IT从业者…

python Bokeh库学习记录

First steps 2: 添加和自定义渲染器 在之前的入门指南中&#xff0c;你使用了Bokeh的figure()函数来绘制折线图。 在本节中&#xff0c;你将使用不同的渲染函数来创建各种其他类型的图表。你还将自定义你的图像外观。 渲染不同的图形符号 Bokeh的绘图界面支持多种不同的图形…

古典显示格式解一偏微分方程并绘制结果的彩色图

解如下偏微分方程 以上公式的Latex代码 \begin{cases}\frac{\partial u}{\partial t}a\frac{\partial ^2u}{\partial x^2}\,\,,0<x<1,t>0\\u\left( x,0 \right) 4x\left( 1-x \right) \,\,,0\leqslant x\leqslant 1\\u\left( 0,t \right) u\left( 1,t \right) 0 ,t\g…

补题篇--codeforces

传送门&#xff1a;Problem - G - Codeforces 题意&#xff1a; 思路&#xff1a; 注意&#xff1a; n 的范围很小&#xff0c;大概率考察状态压缩 因此这个题可以考虑用 状压dp f[i][j] 表示状态为 i 的选法&#xff0c;以第 j 首歌为结尾的播放列表中的歌曲总数 f[ i | …

生活方式对人健康影响非常大 第三篇

身体健康因素中 生活方式占到60% 赶紧去调整自己哪错了 上游的生活方式管理 是药三分毒 药物会影响身体肝肾功能,代谢 所以你要去找上游到底是我哪错了 短板越多 个健康状态越差 饮食管理是生活方式管理中难度最大的 原则1:与基因相对应相平衡 只吃素 会导致大脑萎…

【书生大模型实战营】进阶岛 第6关 MindSearch 快速部署

文章目录 【书生大模型实战营】进阶岛 第6关 MindSearch 快速部署MindSearch 部署到Github Codespace 和 Hugging Face Space创建开发机 & 环境配置MindSearch下载及环境配置获取硅基流动API Key作业 - 基础任务在Github codespaces 启动 MindSearch通过 Github Codespace …

spring框架4 - bean加载

本节内容&#xff1a;beanFactory.getBean("user"); Testvoid testGetBean() {Gun gun beanFactory.getBean("m416", Gun.class);log.info("gun{}", gun);} public <T> T getBean(String name, Class<T> requiredType) throws Bea…

2024年威胁暴露管理两大新趋势

文章目录 前言一、威胁暴露管理的两大新类别二、EAP:减少对CVSS的依赖三、AEV:锁定现实威胁四、CTEM实施面临的挑战与应对策略五、主动风险管理的新时代前言 2024年,安全运营(SecOps)领域迎来重大变革。根据Gartner最新发布的《安全运营技术成熟度曲线》报告,持续威胁暴…

一款人脸识别的芯片内部

三年前在一家3D人脸识别的芯片公司&#xff0c;先后做过两个稍具规模的芯片项目&#xff0c;因为各种原因&#xff0c;这些最终都没有上市&#xff0c;成为沉寂在实验室的产物。但是这些芯片的总体设计都颇具匠心&#xff0c;自己在当时也很有触动&#xff0c;现在拿出一点来供…

揭秘难以复现Bug的解决之道:堆栈分析实战

目录 引言 友情提示难以复现的Bug之痛 寄存器(SP、LR)详解 SP寄存器&#xff1a;堆栈的指路明灯LR寄存器&#xff1a;函数调用与异常处理的桥梁 问题分析与解决流程揭秘 保存现场分析堆栈数据 堆栈结构入栈顺序 案例 J-Link工具 常用命令保存RAM数据到本地 分析栈基本信息 分…

【最新华为OD机试E卷】最大报酬(100分)-多语言题解-(Python/C/JavaScript/Java/Cpp)

🍭 大家好这里是春秋招笔试突围 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-E/D卷的三语言AC题解 💻 ACM金牌🏅️团队| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 🍿 最新华为OD机试D卷目录,全、新、准,题目覆盖率达 95% 以上,…

【Python】企业排名、地域分布与词云分析可视化

目录 数据处理 Pyecharts 各国数量 分布地图 数量占比 城市分布 营业收入 利润转化 数据处理 2021世界五百强企业数据&#xff0c;包含公司名称、公司链接、营业收入(百万美元)、利润(百万美元)、国家等信息。数据集下载&#xff1a;Python企业排名、地域分布与词云分…

opencv-python 图像增强十七:泊松图像融合

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、概述二&#xff0c;实现&#xff1a; 前言 在深入探讨图像处理与计算机视觉领域的过程中&#xff0c;我们不禁对图像融合技术的精妙与实用性感到着迷。图像…

物联网之云平台架构

一&#xff0c;一个典型的物联网云平台 一个典型的物联网&#xff08;IoT&#xff09;云平台需要实现多个功能&#xff0c;以支持物联网设备的接入、数据处理、设备管理、实时控制等需求。 &#xff08;一&#xff09;核心功能 1&#xff0c;设备接入与管理&#xff1a; - 设…

【达梦数据库】DBeaver连接达梦数据库

打开 DBeaver&#xff0c;新建驱动管理器 新建驱动管理器&#xff0c;配置信息如下 添加库文件&#xff0c;jar包使用项目上使用的jdbc驱动包即可&#xff0c;找到本地maven仓库jar位置进行添加。 <dependency><groupId>com.dameng</groupId><artifact…

打开配置好的gee的jupyter Lab环境

目录 打开anconda 打开箭头下的cmd环境&#xff0c;输入jupyter lab

Spring MVC 八股文

目录 重点 SpringMVC的工作原理 Spring MVC 拦截器 Spring MVC 的拦截器和 Filter 过滤器有什么差别&#xff1f; 基础 什么是SpringMVC SpringMVC的优点 Spring MVC的核心组件 Spring MVC的常用注解由有哪些 Controller 注解有什么用 重点 SpringMVC的工作原理 1、客…

人脸静态活体检测(高精度版) API 对接说明

人脸静态活体检测&#xff08;高精度版&#xff09; API 对接说明 本文将介绍人脸静态活体检测&#xff08;高精度版&#xff09;API 对接说明&#xff0c;它可用于对用户上传的静态图片进行防翻拍活体检测&#xff0c;以判断是否是翻拍图片。 接下来介绍下 人脸静态活体检测…

浅谈sizeof() 函数在Arduino中的使用

下面浅谈 sizeof() 函数在Arduino中的使用 注意&#xff1a;这里用sizeof(str[2])&#xff0c;sizeof(str[0])&#xff0c;sizeof(str[1])都是一样的 String str[6]{"abc","defg","hijk","lm","n"}; int num; void setup…

C++项目详细分析_WebServer

前言 项目地址 项目介绍 源码详细分析 项目路径如下&#xff1a; 1.webserver.cpp 头文件和构造函数 #include "webserver.h"WebServer::WebServer() {// http_conn类对象users new http_conn[MAX_FD];// root文件夹路径char server_path[200];getcwd(server…