Pergunta

I'd like to avoid my query to have some redundant code. Here is a very simple example of what id'like to do :

SELECT 
   p.id,
   p.NOM,
   CONCAT(p.NANE,'_2') AS CLONE ,
   (SELECT id FROM person WHERE NAME = CONCAT(p.NOM,'_2')  )

FROM person p

I have 2 times the same code :

CONCAT(PERSON.NAME,'_CLONE')

It is not easy to maintain (the code above is only a simple example of what I'm trying to do).

Is there a solution to get the same result with something like that (wich works in MySQL (tested with v14.14)) :

SELECT 
  p.id,
  p.name,
  CONCAT(p.name,'_clone') as CLONE ,
  (SELECT ID FROM person WHERE name = CLONE) AS CLONE_ID

FROM person p

This code does not work with Oracle 11.2.0.2.0 I am new to oracle, so I don't know what is the translation for the MySQL SQL code above :s

Could you please help me finding a good way of writing it ?

Foi útil?

Solução

I would suggest an inline view:

SELECT p.id,
       p.name,
      c.name clone_name,
      c.id clone_id
from person p
join
      (SELECT id, concat(name, '_clone') name FROM person) c
on p.name = c.name
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top