Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use value from one query into the second one

How can I use the value from one query into the second one. I tried with an alias "papa" but I read that the order of executions makes the alias not available to the second query.

What would be the appropriate way of achieving something like below ?

select id, name, parent_id as papa, (select name from people where id = papa)
from people;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would use a self join for this:

select p.id, 
       p.name, 
       p.parent_id as papa_id,
       papa.name as papa_name
from people p
  left join people papa on p.parent_id = papa.id

Online example

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading