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

推荐订阅源

S
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
Latest news
Latest news
Help Net Security
Help Net Security
S
Security Affairs
Webroot Blog
Webroot Blog
The Hacker News
The Hacker News
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Help Net Security
L
Lohrmann on Cybersecurity
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
MongoDB | Blog
MongoDB | Blog
Cyberwarzone
Cyberwarzone
The Last Watchdog
The Last Watchdog
S
Securelist
N
News and Events Feed by Topic
S
Secure Thoughts
F
Fortinet All Blogs
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
M
MIT News - Artificial intelligence
F
Full Disclosure
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
P
Privacy International News Feed
L
LangChain Blog
Know Your Adversary
Know Your Adversary
C
CERT Recently Published Vulnerability Notes

博客园 - 小纸条

模型微调 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