@ItayMeir Well I actually haven't worked with sql in a while but I have done what you are trying to do in the past.
What I did:
Encapsulating this code I made a loop that I could determine the number of loops I wanted. Then I made a variable that had a count, then decremented it because it was the length of the list you are searching for. I queried the index of that count and pulled all those values.
This whole for loop though was calling a simple function that called this query and stored the value in an array. Then to get the contents out all I needed to do was look through the array. This was of course a temporary array so it never had the same values unless you queried the exact same thing.
Sorry I could not give you exact code on how to fix it but at least an idea if it is of any use to you.