Skip to content

Subquery

一句话解释

subquery 是 SELECT 语句 nested 内部 another SQL 语句 — 它的 result 是 used 通过外部查询.

用大白话说

subquery 是查询内部查询. 内部查询运行首先 and 它的 result 是 used 通过外部查询. Subqueries 能 appear in SELECT 列表, FROM clause, or WHERE clause.

For example, to find users who spent more than average:

SELECT * FROM 顺序 WHERE 总计 > (SELECT AVG(总计) FROM 顺序);

The subquery SELECT AVG(总计) FROM 顺序 computes the average, and the outer query finds orders above that average.

Subqueries can be correlated (referencing the outer query) or uncorrelated (independent). They are powerful but can be slow — sometimes a JOIN is more efficient.

示例

sql
-- Find orders above the average order value
SELECT * FROM orders
WHERE total > (SELECT AVG(total) FROM orders);

subquery computes 平均顺序值; 外部查询 uses 它 as threshold.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表