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

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
博客园_首页
J
Java Code Geeks
V2EX - 技术
V2EX - 技术
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
Forbes - Security
Forbes - Security
Schneier on Security
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
PCI Perspectives
PCI Perspectives
O
OpenAI News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hacker News: Ask HN
Hacker News: Ask HN
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Heimdal Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 聂微东
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
美团技术团队
V
V2EX
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cybersecurity and Infrastructure Security Agency CISA
有赞技术团队
有赞技术团队
腾讯CDC
Cloudbric
Cloudbric
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
SecWiki News
SecWiki News
IT之家
IT之家
C
Cisco Blogs
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
S
Schneier on Security
Security Latest
Security Latest
Scott Helme
Scott Helme
H
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks Blog
L
LINUX DO - 热门话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 小纸条

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