SQL Server - 引用先前的选择

时间:2011-07-16 21:23:04

标签: sql sql-server

我正在学习SQL,所以请耐心等待。我想知道以下是否可行,如果可以,你如何参考以前的选择?

SELECT [col1] as something1, [col2] as something2, 
       <Can I use something1 or something2 in the third selection?> 
  FROM [MyTable] 
 WHERE <Condition>

2 个答案:

答案 0 :(得分:1)

Per this answer我认为这不适用于SQL Server。

如果你真的需要使用别名,你可以这样做:

SELECT yoursubquery.something1, yoursubquery.something2, 
  FROM (
        SELECT [col1] as something1, [col2] as something2 
          FROM [MyTable]
         WHERE <Condition>
       ) yoursubquery

或者更简单地只需在需要时输入完整的列名称:

SELECT [col1] as something1, [col2] as something2, 
       [col1] + [col2] -- or whatever else you wanted to do 
  FROM [MyTable] 
 WHERE <Condition>

答案 1 :(得分:0)

你想如何在第三个选择中使用它们?

你可以这样做:

SELECT
  COL1,
  COL2,
  COL1 + COL2 AS COL3
FROM
  TABLE1
WHERE
  COL1 > 0

但我不清楚“使用”它们的意思。