【问题标题】:Concatenate and use in where clause oracle plsql在where子句oracle plsql中连接和使用
【发布时间】:2014-10-25 13:52:41
【问题描述】:

我必须连接两个字段并在 where 子句中使用连接字段,但它给了我无效的标识符。如何解决这个问题。

select i.FIRST_NAME || ' - ' || i.LAST_NAME as NAME, i.* from CONTACT i 
where NAME = 'JOHN - HANKS'

这给了我

ORA-00904: "NAME": invalid identifier
00904. 00000 -  "%s: invalid identifier"

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    您不能在同一级别使用列别名。只需使用子查询(或重复表达式):

    select c.*
    from (select i.FIRST_NAME || ' - ' || i.LAST_NAME as NAME, i.*
          from CONTACT i 
         ) c
    where c.NAME = 'JOHN - HANKS';
    

    【讨论】:

      【解决方案2】:

      WITH 子句也是一个很好的替代方案,可读性更好。另外,如果要多次使用子查询,那就更好了。

      WITH data as(
         select i.FIRST_NAME || ' - ' || i.LAST_NAME as NAME, i.* from CONTACT i)
      select  * from data where name = 'JOHN - HANKS';
      

      【讨论】:

        【解决方案3】:

        选择 i.FIRST_NAME || ' - ' || i.LAST_NAME 作为 NAME,i.* 来自 CONTACT i 其中 i.first_name ||'~'||i.last_name = 'JHON~HANKS';

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多