惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 小纸条

ruoyiai 启动指南 反向传播 numpy的使用 B 和 B+树 红黑树 ruoyi-vue 梯度下降法 博弈论 离散化 AcWing 907. 区间覆盖 AcWing 906. 区间分组 AcWing 908 最大不相交区间数量 AcWing 905. 区间选点 AcWing 104. 货仓选址 动态规划经典题 窗口函数 1226. 哲学家进餐 1195. 交替打印字符串 1117. H2O 生成 1116. 打印零与奇偶数
关联子查询
小纸条 · 2025-10-27 · via 博客园 - 小纸条

关联子查询是一种依赖外部查询结果的子查询,它的核心特点是:子查询中会引用外部查询的表或字段,因此会针对外部查询的每一行数据,动态执行一次子查询,形成“内外联动”的关系。

关键特点:

  1. 依赖外部查询:子查询的条件中包含外部查询的表字段(如 WHERE 子查询表.字段 = 外部表.字段),无法独立执行。
  2. 逐行执行:外部查询每处理一行数据,子查询就会根据当前行的字段值重新计算一次结果。
  3. 用于“分组内对比”:适合需要基于外部查询的分组(如每个用户、每个类别),动态查询该分组内特定数据的场景。

简单示例:

假设有一张学生成绩表 scores(student_id, subject, score),要查询“每门科目中,分数高于该科目平均分的学生”,可用关联子查询:

SELECT student_id, subject, score
FROM scores s1  -- 外部查询表
WHERE score > (
    -- 关联子查询:引用外部表的subject,计算当前科目的平均分
    SELECT AVG(score) 
    FROM scores s2 
    WHERE s2.subject = s1.subject  -- 子查询依赖外部表的subject
);
  • 外部查询遍历 s1 的每一行(如 (101, 数学, 90));
  • 子查询针对当前行的 subject(数学),计算所有数学成绩的平均分;
  • 外部查询判断当前行的分数是否高于该平均分,符合则保留。

与普通子查询的区别:

  • 普通子查询:独立执行一次,结果固定(如 SELECT * FROM t WHERE id IN (SELECT id FROM t2))。
  • 关联子查询:依赖外部查询,每一行都可能执行一次,结果动态变化。

关联子查询的逻辑直观,但性能可能较低(数据量大时需多次扫描表),适合简单场景;复杂场景建议用窗口函数替代。

例如leetcode的
3421. 查找进步的学生

# -------------关联子查询---------------------------------------
select s1.student_id, s1.subject,
    s1.score as first_score,
    s2.score as latest_score
from scores s1 join scores s2  
    on s1.student_id=s2.student_id and s1.subject=s2.subject
where s1.exam_date=
    (select min(exam_date) from scores where student_id=s1.student_id and subject=s1.subject)
and s2.exam_date=
    (select max(exam_date) from scores where student_id=s2.student_id and subject=s2.subject)
and s1.score<s2.score
group by s1.student_id,s1.subject
order by s1.student_id,s1.subject