简体   繁体   English

DB2 AS/400 iseries 在 where 子句中使用别名

[英]DB2 AS/400 iseries Use alias in where clause

I'm trying to use the alias of a column in the where clause.我试图在 where 子句中使用列的别名。 for example:例如:

SELECT col1 AS alias1, col2 + col3 as sums 
FROM my_table
WHERE sums > 10

But then I get an error message saying:但是后来我收到一条错误消息说:

Column sums not in specified tables.列总和不在指定表中。

Is there anyway I can do this?无论如何我可以做到这一点吗?

If you really want to use alias in WHERE , not the column itself, you can do it with derived table :如果你真的想在WHERE使用别名,而不是列本身,你可以用派生表来做到:

SELECT a.* 
FROM
(
  SELECT lmITNO AS Item_Number, lmBANO AS Lot, lmSTAS AS Status, 
  lmRORN AS Order_Number 
  FROM MVXJDTA.MILOMA 
) a
WHERE a.Status = 'CCC'
....

The where-clause is processed before the select-clause in a statement: where-clause在语句中的select-clause之前处理:

The WHERE clause specifies an intermediate result table that consists of those rows of R for which the search-condition is true. WHERE 子句指定一个中间结果表,该表由 R 中搜索条件为真的那些行组成。 R is the result of the FROM clause of the statement. R 是语句的 FROM 子句的结果。

Re-write the where-clause to reference the actual column name:重新编写where 子句以引用实际的列名:

...
WHERE A.lmSTAS = 'CCC'
...

A common-table-expression can be used to pre-process the select-clause . 公共表表达式可用于预处理select-clause For example:例如:

WITH A AS (SELECT 
    lmITNO AS Item_Number, 
    lmBANO AS Lot, 
    lmSTAS AS Status, 
    lmRORN AS Order_Number 
FROM MVXJDTA.MILOMA)
SELECT A.* FROM A WHERE A.Status = 'CCC'
FETCH FIRST 1000 ROWS ONLY

The columns in a CTE can also be renamed by listing them after the table-identifier . CTE 中的列也可以通过在table-identifier之后列出它们来重命名。 For example:例如:

WITH A (Item_Number, Lot, Status, Order_Number)
AS (SELECT 
    lmITNO, 
    lmBANO, 
    lmSTAS, 
    lmRORN
FROM MVXJDTA.MILOMA)
SELECT A.* FROM A WHERE A.Status = 'CCC'
FETCH FIRST 1000 ROWS ONLY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM