Kraj
Kraj

Reputation: 37

Dynamic values to another SQL statement

Is there a way to combine two SQL queries into a single SELECT query in PostgreSQL?

My requirements are as follows:

SELECT id FROM table1;
SELECT name FROM table2 WHERE table2.id = table1.id;

I think I need to pass values of table1.id in as some sort of dynamic values (loop values) for use in the SELECT statement executed on table2. What is the easiest way to solve this problem, is it possible to do this with stored procedures or functions in PostgreSQL?

Upvotes: 1

Views: 53

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125244

select t1.id, name
from
    table1 t1
    inner join
    table2 t2 using (id)
where t1.id = 1

Upvotes: 3

Related Questions