简单的SQL语句的快速复习

语法的执行顺序
 

select 4 字段列表
 

from 1 表名列表
 

where 2 条件列表
 

group by 3 分组前过滤
 

having 分组后过滤
 

order by 5 排序字段列表
 

limit 6 分页参数


聚合函数
 

count 统计数量
 

max 最大值
 

min 最小值
 

avg 平均
 

sum 总和


分组查询使用例子

1.性别分组,统计数量
 

select gender ,count(*)from emp group by gender
 


 

2.性别分组 统计平均年龄
 

select age ,avg(age) from emp group by gender
 


 

3.查询年龄小于45的员工,并且按照工作地址分组,获取员工数量>=3的工作地址
 

首先
 

按工作地址分组然后获取年龄小于45的员工的地址信息的总数
 

select workaddress,count(*)from emp where age<45 group by workaddress

 

再分组完后进行过滤
 

having count(*)>=3


排序查询使用例子

语法:
 

select 字段列表 from 表名 order by 字段1 排序方式1,字段2,排序方式2;
 


 

排序方式:
 

1.ASC:升序(默认)
 

2.DESC:降序
 


 

例子
 

1.根据年龄进行排序,2.年龄相同,再入职日期降序排序
 

select age from emp order by ageasc,entrydate desc
 

多字段排序:第一个字段相同时再进行第二个字段排序


分页查询
 

语法
 

select 字段列表 from 表名 limit 起始索引,查询记录数;
 

select * from emp limit 0 10


函数

字符串函数
 

concat(s1,s2,s3)字符串的拼接

 

lower(str) 小写


 

upper(str)大写
 


 

lpad(str,n,pad)
 

左填充,用字符串pad对左边进行填充,达到n个字符串长度
 


 

rpad(str,n,pad)
 

右填充

 

trim(str)
 

去掉字符串头部和尾部的空格

 

substring(str,start,len)
 

返回字符串str从str位置起的len个长度的字符串
 


 


 

int类型不能补0,因为是整形但可以补1
 


数值函数
 

ceil(x) 向上取整
 

floor(x) 向下取整
 


 

mod(X,Y) 返回x/y的模
 


 

rand()返回0-1内的随机数
 


 

round(X,Y)四舍五入,保留y位小数
 


日期函数
 

curdate() 日期
 

curtime() 时间
 

now() 现在
 


year(date) 获取指定date的年份
 

month(date) 获取指定date的月份
 


 

day(date) 日期
 


 

date-add(date,interval exprtype)
 

返回这个日期加上一个时间间隔后的时间值
 


 

datediff(date1,date2)
 

返回起始时间date1和结束时间date2之间的天数


流程函数
 

if(value,t,f)
 

true返回t
 

false返回f
 


 

ifnull(value1,value2)不空返回value1,空的话返回value2
 


 

case when then
 


 

case when [val]then [res1] else [defaulse] End
 


 

val为true则返回res1
 

否则返回default默认值
 


 

case [expr] when [val] then [res1] else [default] End
 

end是结束
 

当expr的值等于val时返回res1否则返回default
 


 

使用例子
 

select name,(case workaddress when'北京' then'一线',when‘上海’,then‘一线’ end)as‘工作地址’
 


增删改查

添加数据
 

1.给指定字段添加数据 insert values
 

insert into 表名(字段1 , 字段2) values(值1,值2)
 


 


 

2.给全部字段添加数据(不写出具体字段名)
 

insert into 表名 values(值1,值2)
 


 

3.批量添加数据
 

给特定字段
 

insert into 表名(字段1,字段2)values(值1,值2)(值1,值2);
 

给全部字段
 

insert into 表名 values (值1,值2), (值1,值2), (值1,值2);


更新与删除


 

修改数据:update set
 

update 表名 set 字段名1=值1,字段名2=值2......[where 条件]
 

不写条件where的话就是所有都执行
 


 

删除数据 delete
 

delete from 表名 [where 条件]
 


联合查询(union)

union查询,就是把多次的查询结果合并起来形成一个新的查询结果

 

select 字段列表 from 表a
 

union[all]
 

select 字段列表 from 表b
 


 

分别查询薪资>5000,年龄>50的员工
 

select *from emp where salary>5000
 


 

union all
 


 

select *from emp where age>50
 

但是结果会有重复的,为了去重
 

可以把all去
 


报错情况

select *from emp where salary>5000
 

union all
 

select name from emp where age>50
 

这个会发现报错
 

因为对于联合查询来说。字段表的列数字段类型必须保持一致
 


子查询

子查询
 

又称为 嵌套查询
 

标量子查询
 

查询销售部的所有员工信息
 

1. select id from emp where name='销售部';
 

第一部查询出销售部id等于4
 

2.select *from emp where dept_id=4;
 

要两条指令,但我们想用一条搞定
 

select *from emp where dept_id=(select id from emp where name='销售部');
 



 

列子查询
 

常用操作符

in,not in,any,some,all
 

1
 

select id from dept where name='销售部'or name='市场部';
 

查出的id是1和2
 

然后
 

select* from emp where dept_id in (1,2)
 

或者
 

select* from emp where dept_id in (select id from dept where name='销售部'or name='市场部');
 

2
 

查询比财务部的所有人工资都高的员工的 信息
 

a 查询所有财务部人员的工资
 

select id from dept where name='财务部';
 

select salary from emp where dept_id=3
 


 

b查询比财务部所有人工资都高的员工信息
 

select *from emp where salary> all( select salary from emp where dept_id=3)
 


 

3
 

查询比研发部其中任意一人工资都高的员工信息
 

因为是任意一人所以 没有all
 


行子查询
 

查询与‘张无忌’薪资以及领导都相同的员工的信息


 

a.查询张无忌的工资及其领导
 

select salary,managerid from emp where name='张无忌'


 

b. 查询员工
 

select *from emp where salary=12500 and mangerid =1;
 

或者
 

select *from emp where (salary,managerid)=(12500,1);
 

再或者
 

select *from emp where(salary,mangerid)=(select salary,managerid from emp where name='张无忌')
 



 

表子查询
 

常用操作符 in
 

1.查询与‘路’和‘白’薪资以及职位相同的员工
 

select job,salary from emp where name='路'or name=‘白’
 

select * from emp where (job,salary) in(select job,salary from emp where name='路'or name=‘白’)
 


 

2.查询入职日期是“2006-01-01”之后的员工信息,及其部门信息
 

select * from emp where entrydate>"2006-01-01"
 

把上面那个作为临时表
 

select e.*,d.* from(select * from emp where entrydate>"2006-01-01") e left join dept d on e.dept_id=d.id
 


多表联查

1.查询员工的姓名,年龄,职位,部门信息(隐式内连接)
 

表:emp dept
 

连接条件:emp.dept_id=dept.id
 

记得消除笛卡尔积
 

select e.name,e.age,e.job,d.name from emp e, dept d where e.dept_id=d.id;
 


 

2.查询年龄小于30岁的员工的姓名,年龄,职位,部门信息(显示内连接)
 

select e.name,e.age,e.job,d.name from emp e inner join dept d on e.dept_id=d.id where e.age<30
 


 

3.查询拥有员工的部门id和部门名称
 

求取员工表和部门表之间的交集用内连接
 

select d.id,d.name from emp e,dept d where e.dept_id=d.id
 

此时会有多个重复的部门,因为他是按照员工数量来的
 

去重复用 distinct
 

select distinct d.id,d.name from emp e,dept d where e.dept_id=d.id
 


 

4.查询所有年龄大于40的员工,及其归属部门的名称;如果员工没有分配部门也要显示出来
 

要用外连接
 

select e.*,d.name from emp e left join dept d on e.dept_id=d.id where e.age>40
 


 

5.查询所有员工的工资等级
 

表:emp salarygrade
 

连接条件:emp.salary >=salagrade.losal and emp.salary<=salagrade.hisal
 


 

select e.*,s.grade emp e,salagrade s where e.salary>=s.losal and e.salary <=s.hisal
 


 

第二种写法:
 

select e.*,s.grade emp e,salagrade s where e.salary between s.losal and s.hisal
 


 

6.查询 研发部 所有员工的信息以及工资等级
 

涉及到的表:emp dept salgrade
 

连接条件:
 

emp.salary between s.losal and s.hisal
 


 

emp.dept_id=dept.id
 


 

查询条件 dept.name='研发部'
 


 

select e.*,s.grade from emp e ,dept d,salgrade s where e.dept_id=d.id and ( emp.salary between s.losal and s.hisal)and d.name='研发部'
 


 

7.查询研发部员工的平均工资
 

表 emp dept
 

select avg(e.salary) from emp e, dept d where e.dept_id=d.id and e.name='研发部'
 



 

8.查询工资比‘灭绝’高的员工信息
 

select * from emp where salary>(select salary from emp where name='灭绝')
 

查询灭绝的薪资
 

select salary from emp where e.name='灭绝'
 


 

9.查询比平均薪资高的员工信息
 

select avg(salary) from emp
 

select * from emp where salary>(select avg(salary) from emp)
 


 

10.查询低于 本部门 平均薪资的员工
 

a.查询指定部门的平均薪资
 

select avg(e.salary) from emp e where e.dept_id=1
 

select avg(e.salary) from emp e where e.dept_id=2
 


 

b.
 

select *from emp e2 where salary<(select avg(e.salary) from emp e where e.dept_id=e2.dept_id)
 

保证平均下来的薪资是同一个部门的
 


 

11.查询所有的部门信息,并统计部门的员工人数
 

a.查询信息
 

select id,name from dept
 

b.查询指定部门的人数
 

select count(*) from emp where dept_id=1
 


 

最终
 

select d.id ,d.name (select count(*) from emp e where e.dept_id=id)'人数' from dept d;
 


 


 

12.查询所有学生的选课情况,展示出学生的名称,学号,课程名称
 


 

表:student ,course,student_course
 

连接条件:student.id=student_course.studentid,course.id=student_course.courseid
 


 

select s.name ,s.no,c.name from student s,student_course sc,course c where s.id=sc.studentid and sc.courseid=c.id
 

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微机原理与接口技术期末大作业——4位抢答器仿真

在微机原理与接口技术的学习旅程中&#xff0c;期末大作业成为了检验知识掌握程度与实践能力的关键环节。本次我选择设计并仿真一个 4 位抢答器系统&#xff0c;通过这个项目&#xff0c;深入探索 8086CPU 及其接口技术的实际应用。附完整压缩包下载。 一、系统设计思路 &…

【大模型LLM面试合集】大语言模型架构_MHA_MQA_GQA

MHA_MQA_GQA 1.总结 在 MHA&#xff08;Multi Head Attention&#xff09; 中&#xff0c;每个头有自己单独的 key-value 对&#xff1b;标准的多头注意力机制&#xff0c;h个Query、Key 和 Value 矩阵。在 MQA&#xff08;Multi Query Attention&#xff09; 中只会有一组 k…

【Transformer】手撕Attention

import torch from torch import nn import torch.functional as F import mathX torch.randn(16,64,512) # B,T,Dd_model 512 # 模型的维度 n_head 8 # 注意力头的数量多头注意力机制 class multi_head_attention(nn.Module): def __init__(self, d_model, n_hea…

【Linux】 冯诺依曼体系与计算机系统架构全解

Linux相关知识点可以通过点击以下链接进行学习一起加油&#xff01;初识指令指令进阶权限管理yum包管理与vim编辑器GCC/G编译器make与Makefile自动化构建GDB调试器与Git版本控制工具Linux下进度条 冯诺依曼体系是现代计算机设计的基石&#xff0c;其统一存储和顺序执行理念推动…

冯·诺依曼体系结构

目录 冯诺依曼体系结构推导 内存提高冯诺依曼体系结构效率的方法 你使用QQ和朋友聊天时&#xff0c;整个数据流是怎么流动的&#xff08;不考虑网络情况&#xff09; 与冯诺依曼体系结构相关的一些知识 冯诺依曼体系结构推导 计算机的存在就是为了解决问题&#xff0c;而解…

全面认识了解DeepSeek+利用ollama在本地部署、使用和体验deepseek-r1大模型

文章目录 一、DeepSeek简介二、技术特点三、架构设计3.1、DeepSeek-V33.2、DeepSeek-V23.3、DeepSeek-R1 四、DeepSeek算法4.1、DeepSeek LLM 算法4.2、DeepSeek-V2 算法4.3、DeepSeek-R1 算法4.4、DeepSeek 在算力优化上的算法 五、DeepSeek的使用六、本地部署DeepSeek R1模型…

Python 梯度下降法(七):Summary

文章目录 Python 梯度下降法&#xff08;七&#xff09;&#xff1a;Summary一、核心思想1.1 核心思想1.2 优化方法概述1.3 第三方库的使用 二、 BGD2.1 介绍2.2 torch 库算法2.2 代码示例2.3 SGD2.4 SGD代码示例2.5 MBGD2.6 MBGD 代码示例 三、 Adagrad3.1 介绍3.2 torch 库算…

SpringBoot Web开发(SpringMVC)

SpringBoot Web开发&#xff08;SpringMVC) MVC 核心组件和调用流程 Spring MVC与许多其他Web框架一样&#xff0c;是围绕前端控制器模式设计的&#xff0c;其中中央 Servlet DispatcherServlet 做整体请求处理调度&#xff01; . 除了DispatcherServletSpringMVC还会提供其他…

Web_php_unserialize

代码审计 <?php class Demo { private $file index.php;public function __construct($file) { $this->file $file; }、 //接收一个参数 $file 并赋值给私有属性 $filefunction __destruct() { echo highlight_file($this->file, true); } //在对象销毁时调用&…

Spring Web MVC基础第一篇

目录 1.什么是Spring Web MVC&#xff1f; 2.创建Spring Web MVC项目 3.注解使用 3.1RequestMapping&#xff08;路由映射&#xff09; 3.2一般参数传递 3.3RequestParam&#xff08;参数重命名&#xff09; 3.4RequestBody&#xff08;传递JSON数据&#xff09; 3.5Pa…

安装anaconda3 后 电脑如何单独运行python,python还需要独立安装吗?

安装anaconda3 后 电脑如何单独运行python&#xff0c;python还需要独立安装吗? 电脑第一此安装anaconda用于jupyter notebook使用。 但是在运行cmd的时候&#xff0c;输入python --version 显示未安装或跳转商店提示安装。 明明我可以运行python但是为什么cmd却说我没安装呢…

分布式事务组件Seata简介与使用,搭配Nacos统一管理服务端和客户端配置

文章目录 一. Seata简介二. 官方文档三. Seata分布式事务代码实现0. 环境简介1. 添加undo_log表2. 添加依赖3. 添加配置4. 开启Seata事务管理5. 启动演示 四. Seata Server配置Nacos1. 修改配置类型2. 创建Nacos配置 五. Seata Client配置Nacos1. 增加Seata关联Nacos的配置2. 在…

使用真实 Elasticsearch 进行高级集成测试

作者&#xff1a;来自 Elastic Piotr Przybyl 掌握高级 Elasticsearch 集成测试&#xff1a;更快、更智能、更优化。 在上一篇关于集成测试的文章中&#xff0c;我们介绍了如何通过改变数据初始化策略来缩短依赖于真实 Elasticsearch 的集成测试的执行时间。在本期中&#xff0…

OpenEuler学习笔记(十四):在OpenEuler上搭建.NET运行环境

一、在OpenEuler上搭建.NET运行环境 基于包管理器安装 添加Microsoft软件源&#xff1a;运行命令sudo rpm -Uvh https://packages.microsoft.com/config/centos/8/packages-microsoft-prod.rpm&#xff0c;将Microsoft软件源添加到系统中&#xff0c;以便后续能够从该源安装.…

基于Python的简单企业维修管理系统的设计与实现

以下是一个基于Python的简单企业维修管理系统的设计与实现&#xff0c;这里我们会使用Flask作为Web框架&#xff0c;SQLite作为数据库来存储相关信息。 1. 需求分析 企业维修管理系统主要功能包括&#xff1a; 维修工单的创建、查询、更新和删除。设备信息的管理。维修人员…

Van-Nav:新年,将自己学习的项目地址统一整理搭建自己的私人导航站,供自己后续查阅使用,做技术的同学应该都有一个自己网站的梦想

嗨&#xff0c;大家好&#xff0c;我是小华同学&#xff0c;关注我们获得“最新、最全、最优质”开源项目和高效工作学习方法 Van-Nav是一个基于Vue.js开发的导航组件库&#xff0c;它提供了多种预设的样式和灵活的配置选项&#xff0c;使得开发者可以轻松地定制出符合项目需求…

Android 音视频编解码 -- MediaCodec

引言 如果我们只是简单玩一下音频、视频播放&#xff0c;那么使用 MediaPlayer SurfaceView 播放就可以了&#xff0c;但如果想加个水印&#xff0c;加点其他特效什么的&#xff0c;那就不行了&#xff1b; 学习 Android 自带的硬件码类 – MediaCodec。 MediaCodec 介绍 在A…

UE 5.3 C++ 对垃圾回收的初步认识

一.UObject的创建 UObject 不支持构造参数。 所有的C UObject都会在引擎启动的时候初始化&#xff0c;然后引擎会调用其默认构造器。如果没有默认的构造器&#xff0c;那么 UObject 将不会编译。 有修改父类参数的需求&#xff0c;就使用指定带参构造 // Sets default value…

使用LLaMA-Factory对AI进行认知的微调

使用LLaMA-Factory对AI进行认知的微调 引言1. 安装LLaMA-Factory1.1. 克隆仓库1.2. 创建虚拟环境1.3. 安装LLaMA-Factory1.4. 验证 2. 准备数据2.1. 创建数据集2.2. 更新数据集信息 3. 启动LLaMA-Factory4. 进行微调4.1. 设置模型4.2. 预览数据集4.3. 设置学习率等参数4.4. 预览…

2025最新源支付V7全套开源版+Mac云端+五合一云端

2025最新源支付V7全套开源版Mac云端五合一云端 官方1999元&#xff0c; 最新非网上那种功能不全带BUG开源版&#xff0c;可以自己增加授权或二开 拥有卓越的性能和丰富的功能。它采用全新轻量化的界面UI&#xff0c;让您能更方便快捷地解决知识付费和运营赞助的难题 它基于…