sql 带有FROM子句的子查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 带有FROM子句的子查询相关的知识,希望对你有一定的参考价值。

\!h Example - convert years to months in producer list:
SELECT producer_name, months FROM -- returns producer_name and months from the subquery
-- subquery returns two columns, producer_name and months(alias for years multiplied by 12), from the producer table
(SELECT producer_name, years*12 AS months FROM producer) AS prod; -- 2nd alias here needs to be included
+---------------+--------+
| producer_name | months |
+---------------+--------+
| Phil Spector  |    432 |
| George Martin |    480 |
| Tina Weymouth |    240 |
| Chris Frantz  |    240 |
| Ed Kuepper    |    180 |
+---------------+--------+

\!h Example 2 - average number of albums we own by each artist:
-- avg of 'albums' subquery
SELECT AVG(albums) FROM 
-- subquery returns no. of unique albums and aliases it 'albums'
(SELECT COUNT(*) AS albums FROM artist INNER JOIN album
USING (artist_id) GROUP BY artist.artist_id) AS alb;
+-------------+
| AVG(albums) |
+-------------+
|      2.1667 |
+-------------+
\!h These subqueries are a good way to apply two aggregate functions to one set of data
-- This is because things like AVG(COUNT(*)) wont work.

以上是关于sql 带有FROM子句的子查询的主要内容,如果未能解决你的问题,请参考以下文章

带有联合错误的 MySQL 视图 - “视图的 SELECT 包含 FROM 子句中的子查询”

使用 FROM 子句中的子查询进行联接操作中的 SQL 语法错误

FROM中的子查询不在Oracle SQL中工作

在 MS Query 中使用多个 INNER JOIN 的 FROM 子句中的子查询的语法

带有(来自)别名的 SQL 子查询

SQL 编译错误:无法评估不受支持的子查询类型 - SELECT 子句中的函数调用