SQL 从子查询中选择 *
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8911654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select * from subquery
提问by karel
I'd like to get sum of column1, sum of column2 and total sum. In Postgres I can do it this way: (notice the star)
我想得到 column1 的总和,column2 的总和和总和。在 Postgres 中,我可以这样做:(注意星号)
SELECT *, a+b AS total_sum FROM
(
SELECT SUM(column1) AS a, SUM(column2) AS b
FROM table
)
But in Oracle I get an syntax error and have to use this:
但是在 Oracle 中我得到一个语法错误并且必须使用这个:
SELECT a,b, a+b AS total_sum FROM
(
SELECT SUM(column1) AS a, SUM(column2) AS b
FROM table
)
I have really lot of columns to return so I do not want to write the column names again in the main query. Is there any easy solution?
我真的有很多列要返回,所以我不想在主查询中再次写入列名。有什么简单的解决办法吗?
I cannot use a+b in the inner query because there are not known at this place.
I do not want to use SELECT SELECT SUM(column1) AS a, SUM(column2) AS b, SUM(column1)+SUM(column2) AS total_sum
.
我不能在内部查询中使用 a+b,因为在这个地方不知道。我不想使用SELECT SELECT SUM(column1) AS a, SUM(column2) AS b, SUM(column1)+SUM(column2) AS total_sum
.
回答by Peter Lang
You can select every column from that sub-query by aliasing it and adding the alias before the *
:
您可以通过为该子查询设置别名并在 之前添加别名来选择该子查询中的每一列*
:
SELECT t.*, a+b AS total_sum
FROM
(
SELECT SUM(column1) AS a, SUM(column2) AS b
FROM table
) t