如何从子选择中选择无别名的数字文字
我想知道是否有通用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在Oracle中,你可以这样做
In Oracle, you can do
根据 ANSI-92 标准,它取决于实现。从第 7.9、9.c 节:
换句话说,这一切都取决于您当时使用的 RDBMS。
顺便说一句,您可以查看 ANSI-92 标准你正在寻找一些有趣的阅读。
According to the ANSI-92 standard it is implementation dependent. From section 7.9, 9.c:
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.