简体   繁体   English

连接并在 where 子句中使用 oracle plsql

[英]Concatenate and use in where clause oracle plsql

I have to concatenate two fields and use concatenated field in where clause but it gives me invalid identifier.我必须连接两个字段并在 where 子句中使用连接字段,但它给了我无效的标识符。 How to solve this query.如何解决此查询。

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

This gives me这给了我

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

You cannot use a column alias at the same level.您不能在同一级别使用列别名。 Just use a subquery (or repeat the expression):只需使用子查询(或重复表达式):

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

The WITH clause is also a good alternative, better readability. WITH 子句也是一个很好的选择,更好的可读性。 Also, if the subquery is to be used multiple times, it is even better.另外,如果子查询要被多次使用,那就更好了。

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

select i.FIRST_NAME ||选择 i.FIRST_NAME || ' - ' || ' - ' || i.LAST_NAME as NAME, i.* from CONTACT i where i.first_name ||'~'||i.last_name = 'JHON~HANKS'; i.LAST_NAME as NAME, i.* from CONTACT i where i.first_name ||'~'||i.last_name = 'JHON~HANKS';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM