如何从子选择中选择无别名的数字文字

发布于 2024-11-19 11:32:01 字数 893 浏览 2 评论 0原文

我想知道是否有通用的 SQL 语法允许从子选择中选择无别名的数字文字:

-- Seems to work in MySQL / Oracle
select table_alias."1"
from (
   select 1 from dual
) table_alias

我知道我可以为子选择中的字段添加别名:

-- Works everywhere
select table_alias.column_alias
from (
   select 1 column_alias from dual
) table_alias

但是如果我无法控制子选择怎么办?另外,某些 RDBMS 允许在为表别名时同时提供表别名和列别名:

-- Seems to work in Postgres / SQL Server
select table_alias.column_alias
from (
  select 1 from dual
) table_alias (column_alias)

但某些 RDBMS(例如 MySQL)不能这样做。还有别的办法吗?

  • 注意:这不是关于任何特定的 RDBMS,而只是一般的 SQL
  • 注意:我想省略星号,即没有 select *...

一个相关的问题是这里:

< a href="https://stackoverflow.com/questions/14127707/is-there-a-generic-workaround-to-express-a-driven-column-list-in-oracle-and-my">是否有通用解决方法在 Oracle(和 MySQL)中表达派生列列表?

I'm wondering if there is a general SQL syntax allowing for selecting unaliased numeric literals from sub-selects:

-- Seems to work in MySQL / Oracle
select table_alias."1"
from (
   select 1 from dual
) table_alias

I know I could alias the fields in the subselect:

-- Works everywhere
select table_alias.column_alias
from (
   select 1 column_alias from dual
) table_alias

But what if I don't have control over the subselect? Also, some RDBMS allow to provide both table AND column aliases when aliasing tables:

-- Seems to work in Postgres / SQL Server
select table_alias.column_alias
from (
  select 1 from dual
) table_alias (column_alias)

But some RDBMS (e.g. MySQL) can't do that. Is there another way?

  • Note: This isn't about any specific RDBMS, but just SQL in general
  • Note: I'd like to omit the asterisk, i.e. no select *...

A related question is this one here:

Is there a generic workaround to express a derived column list in Oracle (and MySQL)?

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

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

发布评论

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

评论(2

方圜几里 2024-11-26 11:32:02

在Oracle中,你可以这样做

select aa."2" from ( select 2 from dual ) aa

In Oracle, you can do

select aa."2" from ( select 2 from dual ) aa
云之铃。 2024-11-26 11:32:01

根据 ANSI-92 标准,它取决于实现。从第 7.9、9.c 节:

否则,<列名称>的第 i 列的值取决于实现,并且与
<栏目名称>引用的表中除其自身之外的任何列
通过任何<表参考>包含在 SQL 语句中。

换句话说,这一切都取决于您当时使用的 RDBMS。

顺便说一句,您可以查看 ANSI-92 标准你正在寻找一些有趣的阅读。

According to the ANSI-92 standard it is implementation dependent. From section 7.9, 9.c:

Otherwise, the <column name> of the i-th column of the is implementation-dependent and different from the
<column name> of any column, other than itself, of a table referenced
by any <table reference> contained in the SQL-statement.

In other words, it's all going to depend on the RDBMS that you're using at the time.

BTW, you can check out the ANSI-92 standards if you're looking for some fun reading.

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