Skip to content
Advertisement

Execute same query multiple times with different parameter postgressql

I don’t know how to easily explain what i need to do but i’ll try.

Lets say i have a user table with 5 users.

instead of just do a select * from users i need to do this different and more difficult.

I need to build a query that for each row in users table runs a query with a parameter (name) and then gives me the same output as select * from users

I know it’s sounds wierd but this is what i actually needs to do..

So what i want to happen is this:

  1. I need to loop thru the users table to find out how many rows there is. (5) -That’s the amount of times I want to execute the query.

  2. For each execution of the Query i need to change name in the where clause. First execution = Steve, second = Pat and so on.

  3. In the end I want just one output with everything together so I need to union the result.

If i would this manually it would look like this:

In my real case i need separate queries so a in ('Steve', 'Pat') or a solution like that won’t work.

I hope you understand what im looking for, and if you have any question please ask. Im using postgres v.10

Advertisement

Answer

This should work as you intend to.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement