Oracle 自动重命名 select 中的列?

发布于 2024-09-25 06:37:56 字数 893 浏览 1 评论 0原文

我有 2 个带有以下字段的表。

表 1

  • AA
  • BB
  • CC
  • DD

表 2

  • AA
  • CC
  • EE

查询

Select t1.*, 
       t2.*
  from table1 t1,
  join table2 t2 on table1.DD = table2.EE

我的数据列返回以下列名称:

AA, BB, CC, DD, **AA_1**, **CC_1**, EE

我不想要这样的列名称。我希望它们的表名称以公共名称(或所有列)为前缀。我可以用以下方法解决这个问题:

select t1.AA as t1_AA, t1.BB as t1_BB, t1.CC as t1_CC, t1.DD as t1_DD, 
 t2.AA as t2_AA, t2.CC as t2_CC, t2.EE as t2_EEE
   from table1 t1,
    inner join table2 t2
    on table1.DD = table2.EE

但这意味着每个地方的选择都会长 500 行。 oracle 有什么神奇的方法可以做到这一点吗?基本上我想编写我的代码,例如

 select t1.* as t1_*, t2.* as t2_*
       from table1 t1,
        inner join table2 t2
        on table1.DD = table2.EE

但是当然这不是有效的 SQL

I have 2 tables with the following fields.

Table1

  • AA
  • BB
  • CC
  • DD

Table2

  • AA
  • CC
  • EE

Query

Select t1.*, 
       t2.*
  from table1 t1,
  join table2 t2 on table1.DD = table2.EE

My data columns back with the following column names:

AA, BB, CC, DD, **AA_1**, **CC_1**, EE

I don't want the column names like that. I want them to have the table name prefixed in the names of common (or all columns). I could fix this with:

select t1.AA as t1_AA, t1.BB as t1_BB, t1.CC as t1_CC, t1.DD as t1_DD, 
 t2.AA as t2_AA, t2.CC as t2_CC, t2.EE as t2_EEE
   from table1 t1,
    inner join table2 t2
    on table1.DD = table2.EE

But that means every select everywhere becomes 500 lines longer. Is there a magic way to do this in oracle? Basically I want to write my code like

 select t1.* as t1_*, t2.* as t2_*
       from table1 t1,
        inner join table2 t2
        on table1.DD = table2.EE

But of course that is not valid SQL

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

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

发布评论

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

评论(4

微暖i 2024-10-02 06:37:56

在 Oracle SELECT 语法中,目前无法根据某些表达式将列别名分配给多个列。您必须为每个单独的列分配一个别名。

In Oracle SELECT syntax, there is currently no way to assign column aliases to multiple columns based on some expression. You have to assign an alias to each individual column.

帅哥哥的热头脑 2024-10-02 06:37:56

oracle中有没有一种神奇的方法可以做到这一点?

据我所知。您的选项相当于:

  1. 解决列命名方案 - 您需要使用 ALTER TABLE 语句,例如:

    ALTER TABLE 表名
         将列旧名称重命名为新名称;
    
  2. 使用列别名

您可以使用视图来节省工作和时间。定义列别名的努力,但这不是推荐的做法,因为将视图分层时性能较差。

Is there a magic way to do this in oracle?

Not that I'm aware of. Your options amount to:

  1. Address the column naming scheme - you'd need to use ALTER TABLE statements like:

    ALTER TABLE table_name
         RENAME COLUMN old_name to new_name;
    
  2. Use column aliases

You could use views to save on the work & effort of defining column aliases, but it's not a recommended practice because of the bad performance when layering views on top of one another.

2024-10-02 06:37:56

创建视图是一个选项吗?

您正在使用什么软件可以做到这一点?我在 10g 的 SQL*Plus 或 PL/SQL Developer 中没有看到这种行为。 PL/SQL 不允许您构建带有这种歧义的游标。

Is creating a view an option?

What is the software you're using that does this to you? I don't see this behavior in SQL*Plus or PL/SQL Developer in 10g. PL/SQL won't let you build a cursor with this ambiguity in it.

划一舟意中人 2024-10-02 06:37:56

试试这个

 select t1.AA "t1_AA", t2.AA "t2.AA"
 from table1 t1,
 inner join table2 t2
 on table1.DD = table2.EE

正如他之前所说,你需要每列执行此操作

Try this

 select t1.AA "t1_AA", t2.AA "t2.AA"
 from table1 t1,
 inner join table2 t2
 on table1.DD = table2.EE

As he said before, you need to do it per column

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