为什么在 Oracle 中,当我们从别名获取值时,我们无法将列指定为别名?

发布于 2024-11-07 12:15:59 字数 776 浏览 1 评论 0原文

对于前。 我的查询:

select * 
from 
    (SELECT distinct b.lname||' '||b.fname as "Manager",b.EMPID as "Mgrid" 
     from EMPLOYEE1) a, 
    (select lname,fname,empid from EMPLOYEE1) b 
where b.EMPID=a.MGRID) 

正在获取,

Manager           Mgrid    
----------------  -------- 
Farooque Umer     104      
Sontireddy Kiran  107      
Chopra Bhupendra  103  

但我无法写入:

select Manager, Mgrid 
from 
    (select distinct b.lname||' '||b.fname as "Manager",b.EMPID as "Mgrid" 
    from EMPLOYEE1) a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
where b.EMPID=a.MGRID)

错误:

[错误] 脚本行:1-1 -------------------------- ORA-00904: “MGRID”: 无效标识符

For ex.
My query :

select * 
from 
    (SELECT distinct b.lname||' '||b.fname as "Manager",b.EMPID as "Mgrid" 
     from EMPLOYEE1) a, 
    (select lname,fname,empid from EMPLOYEE1) b 
where b.EMPID=a.MGRID) 

is fetching

Manager           Mgrid    
----------------  -------- 
Farooque Umer     104      
Sontireddy Kiran  107      
Chopra Bhupendra  103  

but I cant write :

select Manager, Mgrid 
from 
    (select distinct b.lname||' '||b.fname as "Manager",b.EMPID as "Mgrid" 
    from EMPLOYEE1) a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
where b.EMPID=a.MGRID)

Error:

[Error] Script lines: 1-1 --------------------------
ORA-00904: "MGRID": invalid identifier

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

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

发布评论

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

评论(3

长途伴 2024-11-14 12:15:59

因为一旦您开始将名称括在双引号(例如“Mgrid”)中,它们就会区分大小写。所以不要。这样做:

select Manager, Mgrid 
from 
    (select distinct b.lname||' '||b.fname as Manager,b.EMPID as Mgrid 
    from EMPLOYEE1) a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
where b.EMPID=a.MGRID)

或者如果你真的喜欢区分大小写的名称,你必须这样做:

select "Manager", "Mgrid" 
from 
    (select distinct b.lname||' '||b.fname as "Manager",b.EMPID as "Mgrid" 
    from EMPLOYEE1) a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
where b.EMPID=a."Mgrid")

Because as soon as you start enclosing names in double quotes like "Mgrid" they becone case-sensitive. So don't. Do this:

select Manager, Mgrid 
from 
    (select distinct b.lname||' '||b.fname as Manager,b.EMPID as Mgrid 
    from EMPLOYEE1) a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
where b.EMPID=a.MGRID)

or if you really like case-sensitive names you must do this:

select "Manager", "Mgrid" 
from 
    (select distinct b.lname||' '||b.fname as "Manager",b.EMPID as "Mgrid" 
    from EMPLOYEE1) a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
where b.EMPID=a."Mgrid")
七月上 2024-11-14 12:15:59

您应该只在顶层选择应用修饰字段命名 - 所有其他标识符都应保留为有效的 Oracle 标识符。

select manager as "Manager", mgrid as "Mgrid" 
from 
    (select distinct b.lname||' '||b.fname as manager, b.EMPID as mgrid
    from EMPLOYEE1) a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
where b.EMPID=a.MGRID)

You should only apply cosmetic field naming at the top level select - all other identifiers should be kept as valid Oracle identifiers.

select manager as "Manager", mgrid as "Mgrid" 
from 
    (select distinct b.lname||' '||b.fname as manager, b.EMPID as mgrid
    from EMPLOYEE1) a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
where b.EMPID=a.MGRID)
淡淡の花香 2024-11-14 12:15:59

除了引号之外,我认为您的帖子还多了一个括号:

select Manager, Mgrid 
from 
    (select distinct b.lname||' '||b.fname as Manager,b.EMPID as Mgrid 
    from EMPLOYEE1 a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
    where b.empid =a.Mgrid)

Besides the quotes, I think your post contains one more parenthesis than needed:

select Manager, Mgrid 
from 
    (select distinct b.lname||' '||b.fname as Manager,b.EMPID as Mgrid 
    from EMPLOYEE1 a, 
    (select lname,fname,empid 
    from EMPLOYEE1) b 
    where b.empid =a.Mgrid)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文