拾光记录
218个文档 导航 作者 Gitee
随笔记录4
Hexo博客:基础使用Hexo博客:Next主题Hexo博客:Next进阶使用Hexo博客:Next高级配置
前端知识4
基础知识17
Vue框架19
UniApp14
微信小程序1
Java编程6
Java基础15
SpringBoot31
SpringMVC18
MyBatis9
SpringCloud15
中间件2
数据库4
MySQL13
Redis8
MongoDB10
其他数据库1
Python编程6
Python基础知识Python语法yolo目标检测OpenCV的使用及树莓派平台condauv管理工具
Linux12
Linux常用命令Jar启动脚本VirtualBox安装CentOSVirtualBox安装Ubuntu树莓派安装及使用frp内网穿透ArchLinux:基础系统安装ArchLInux:图形化界面安装ArchLinux:常用软件ArchLinux:深度优化ArchLinux:NiriArchLinux:模块记录
软件工具12
IDEAGitMavenGradleNginx安装Nginx配置JMeter压测OllamaRustFSPicGoVSCodeDocker
创意设计2
Blender:入门知识UI设计基础知识
AI相关9
Claude CodeHermes AgentOpenAI基本使用OpenAI工具调用OpenAI记忆管理OpenAI推理执行OpenAI开发框架Langchainllama.cpp

表联系

通常多对多的关系会有一张单独的关系表

连接查询

-- 内连接
select * from a,b where a.id=b.aid

-- 左外连接
select a.*,b.name from a left join b on a.id = b.aid

-- 右外连接
select a.*,b.name from a right join b on a.id = b.aid

联合查询

-- union all
select * from a where xxx
union all
select * from a where xxx


-- union
select * from a where xxx
union
select * from a where xxx

union all将上下两个数据直接拼接,union会去重,注意上下查询字段需要一致才能合并

子查询

标量子查询:子查询的结果是个单值,结果可直接用于其他查询,支持的操作符:= > < >= <= <><>是不等于

-- 查询技术部的员工
select * from emp where dept_id = (select id from dept where name = '技术部')
-- 查询某某之后入职的员工
select * from emp where entrydate > (select entrydate from emp where name = '某某')

列子查询:子查询的结果为多个数据(一列),支持的操作符:in not in any some all

操作符 说明
in 在集合范围内
not in 不在集合范围内
any 子查询返回列表中,有任意一个满足即可
some 与any相同
all 子查询返回列表的所有值都要满足
-- 查询“技术部”和“财务部”的所有员工
select * from emp where dept_id in (select id from dept where name = '技术部' or name = '财务部')
-- 查询比“财务部”所有人工资都高的员工信息
select * from emp where salary > all (select salary from emp where dept_id = (select id from dept where name = '财务部'))

行子查询:子查询返回结果为多列一行(多个字段),支持操作符= <> in not in

-- 查询与某某的薪资及直属领导相同的员工信息
select * from emp where (salary,manager_id) = (select salary, manager_id from emp where name = '某某')

表子查询:子查询返回的结果为多列多行,支持操作符in

--查询与“某某一”、“某某二”的职位和薪资相同的员工信息
select * from emp where (job,salary) in (select job,salary from emp where name = '某某一' or name = '某某二')

事务

事务的四大特性:

并发事务问题:

事务隔离级别:

隔离级别 名称 脏读 不可重复读 幻读
Read uncommitted 读未提交
Read committed 读已提交 x
Repeatable Read(默认) 可重复读 x x
Serializable 串行化 x x x

Serializable隔离级别能解决所有事务问题,但是性能不高。即隔离级别越高数据越安全,但是性能越低

-- 查看事务隔离级别
select @@transaction_isolation
-- 设置事务隔离级别
set [session|global] transaction isolation level {级别}
表联系连接查询联合查询子查询事务