How to select an unaliased numeric literal from a sub-select

南笙酒味 提交于 2019-12-24 09:47:41

问题


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)?


回答1:


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.




回答2:


In Oracle, you can do

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


来源:https://stackoverflow.com/questions/6669088/how-to-select-an-unaliased-numeric-literal-from-a-sub-select

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!