Home > front end >  looping over a simple query with different values of "id"
looping over a simple query with different values of "id"

Time:10-01

I have a table with distinct id's that I want to use one by one for my query. Here is the pseudocode of what I am trying to do:

select distinct id
from table1

-- for every distinct id above, use that "id" in the query below - 

select xyz... 
where num = id

I want to do this using loops without using function / procedures etc.

The results for each loop run should be appended to the previous results.

CodePudding user response:

There is no need to use a loop if you need all records to be appended. you can use a subquery as below:

select * from table2
where num in (select distinct id
from table1)
  • Related