Edit:
I removed the confusing stuff and I wanted to simplify what I want to accomplish:
Let’s say John had 2 baskets and 1 of them contained 1 stone, 2 marbles and 2 sticks, the other one contained 2 stones and 2 sticks.
Eric had 1 basket which contained 3 bottles, 2 caps and 1 stone.
Meanwhile Jack had 1 basket which contained 1 stick. Also, Jack has a stone somewhere but it’s not inside his basket yet.
For the purpose of this query, let’s say we’re focusing on Jack’s items. So we would go over everybody’s baskets and see which basket looks pretty much the same as all the items he has overall (a) without thinking whether Jack’s items are inside a basket or not and (b) only compare to Jack’s items with items that are inside other people’s baskets, not outside.
This would be the ideal end result when searching which baskets Jack can match the easiest with all his items regardless of where his items are currently:
Fact:
- Jack has in total 1 stick and 1 stone.
Result:
- 1st: John’s second basket containing 2 stones and 2 sticks
- 2nd: John’s first basket containing 1 stone, 2 marbles and 2 sticks
- 3rd: Eric’s basket containing 3 bottles, 2 caps and 1 stone
This will do what you asked for a single basket:
It’s probably possible to join this to the users table to do all his baskets at once. Then wrap it in yet another query to find the best match.
Try this first, and let me know if it works. If it does I’ll see if I can do that part tomorrow.