Answer the question
In order to leave comments, you need to log in
How to optimize sql queries and get rid of the cycle?
I have a query like:
select a.field1, a.field2, a.field3, b.field1, b.field2
from table1 as a
left join table2 as b on a.field1 = b.field1
where ...
foreach $obj as o {
select c.field1
from table3 as c
left join table4 as d on c.field2 = d.field2
where c.field3 = a.field1
}
Answer the question
In order to leave comments, you need to log in
What prevents you from expanding the original query with the necessary joins and getting exactly the right list?
From the result of the first query, select all a.field1 into the array.
Run the second query with a selection of all rows through where c.field3 in (array a.field1)
Total 2 queries
Or immediately join into one, what is the problem? (Depending on how many rows there should be in this cyclic query)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question