Hi I need some help with the following scenario in php. I have a db with users every user has an ID, have_card and want_card. I know how to make a direct match (one user trades with another user). But if there is no direct match but there is a circular swap like:
User #1 has card A wants card B
User #2 has card B wants card C
User #3 has card C wants card A
In this scenario there is no direct match between two users. But if:
User #1 gives his card to User #3
User #3 gives his card to User #2
User #2 gives his card to User #1
Every ones happy.
All the info I have to start with is User #1 how do I find User #2 and User #3?
Thanks to everyone for your answers.
This is a challenging scenario. I had to build this for a book swapping application a while back and using a white board I was able to more easily visual and solve the problem.
What I did was use the same table inside the query multiple times, just using a different name.
In my scenario, the books that were needed were in a separate table from the books the users had. Hopefully you’ll be able to see the logic in this query: