将表别名与“Where”组合起来在 Informix 中

发布于 2024-10-19 01:52:30 字数 428 浏览 5 评论 0原文

我在 Informix 上使用带有 where 部分的表别名时遇到严重问题。

所以我可以运行:

SELECT ColA, ColB 
  FROM Table1 AS TestTable

但是一旦我尝试这个:

SELECT ColA, ColB 
  FROM Table1 
 WHERE type = 'A' AS TestTable

..它就行不通。当我尝试使用 WHERE 部分连接表时,也会发生同样的情况。

我正在尝试优化这个问题的查询,所以我正在尝试不要使用临时表。

I'm facing serious problems using table aliases on Informix with a where part.

So I can run:

SELECT ColA, ColB 
  FROM Table1 AS TestTable

But as soon as i try this:

SELECT ColA, ColB 
  FROM Table1 
 WHERE type = 'A' AS TestTable

..it wouldn't work. The same happens when I try to join tables with a WHERE part.

I'm trying to optimize a query from this Question so I'm trying not to work with temporary tables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫弦 2024-10-26 01:52:30

我假设您正在尝试获得相当于 Oracle 的内联视图。语法是:

select *
from  
   TABLE(MULTISET(SELECT ColA, ColB 
  FROM Table1 
 WHERE type = 'A' )) T1
join 
   TABLE(MULTISET(SELECT ColA, ColB 
  FROM Table2 
 WHERE type = 'Z' )) T2 on (t2.colA = t1.colA)

I assume you are trying to get the equivalent of Oracle's inline views. Syntax is:

select *
from  
   TABLE(MULTISET(SELECT ColA, ColB 
  FROM Table1 
 WHERE type = 'A' )) T1
join 
   TABLE(MULTISET(SELECT ColA, ColB 
  FROM Table2 
 WHERE type = 'Z' )) T2 on (t2.colA = t1.colA)
只为守护你 2024-10-26 01:52:30

您可以为选择列表中的列或 FROM 子句中的表(或子查询)添加别名。

因此,您可以写:

SELECT ColA, ColB 
  FROM Table1 AS TestTable
 WHERE type = 'A'

或者您可以写:

SELECT t.*, r.*
  FROM (SELECT ColA, ColB 
          FROM Table1 AS TestTable
         WHERE type = 'A'
       ) AS t
  JOIN AnotherTable AS r ON t.ColA = r.ColC

请注意,在此查询中,名称 TestTable 实际上并未使用。更复杂的子查询很可能会使用别名(尽管我非常喜欢短别名 - 比使用别名的表名短的别名)。

You can alias columns in the select-list or tables (or sub-queries) in the FROM clause.

Therefore, you'd write:

SELECT ColA, ColB 
  FROM Table1 AS TestTable
 WHERE type = 'A'

or you'd write:

SELECT t.*, r.*
  FROM (SELECT ColA, ColB 
          FROM Table1 AS TestTable
         WHERE type = 'A'
       ) AS t
  JOIN AnotherTable AS r ON t.ColA = r.ColC

Note that in this query, the name TestTable does not actually get used. A more complex sub-query might well make use of an alias (though I have a strong preference for short aliases — aliases that are shorter than the table name that is aliassed).

静水深流 2024-10-26 01:52:30

据我所知,任何数据库系统都不允许尝试在Where子句中使用别名。您可以使用 Select ... Into 将 Select 语句的结果推送到表中,如下所示:

Select ColA, ColB Into Table2
From Table1 As TestTable

但是,您需要使用单独的 SQL 语句来使用 Table2在上面的例子中。另一种选择是使用公共表表达式,我相信 Informix 支持该表达式:

With Foo As
    (
    Select ColA, ColB
    From Table1 As TestTable
    )
Select ColA, ColB
From Foo

As far as I know, trying to use an alias in a Where clause is not permitted in any database system. You can push the results of a Select statement into a table using Select ... Into like so:

Select ColA, ColB Into Table2
From Table1 As TestTable

However, you would then need to use a separate SQL statement to use Table2 in the above example. The other choice is to use a common-table expression which I believe is supported by Informix:

With Foo As
    (
    Select ColA, ColB
    From Table1 As TestTable
    )
Select ColA, ColB
From Foo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文