A column in my MySQL setup is called favColors. It’s just a VARCHAR column where I’m saving a JSON string with the favColors for each user. Pretty simple.
Let’s say this is User A’s favColors:
["red","green","cyan","silver","gold"]
And this is user B’s favColors:
["pink","green","blue","brown","yellow"]
And this is user C’s favColors:
["tan","green","blue","brown","yellow","violet"]
And user B wants to query to find other users that share the same colors. But i’m interested in finding the BEST match.
I figure I could use something like this…
WHERE MATCH (favColors) AGAINST ('$userBsJSONString' IN BOOLEAN MODE)
but, I’d like to have the results retuned ordered by which rows had the most matches, and id like to know what colors they were a match on.
So basically I could do something like “hey you both like “green”,”blue”,”brown”,”yellow”!
What would be the best way to go about a query that can return this information?
You can’t with the current data layout.
You’ve get extremely un-normalised data here. To do the kind of analysis you want, you need two more tables.
The first table would be of colour. Each colour would have a unique ID and the colour name.
The next table would be an association table. Each row would be a user ID and a colour ID, indicating that that user likes that colour.
Then, a query like:
For each
userID, you’d have a count of the number of colours that matched. The higher the number, the better the match