I am building a bot that matches users based on a score they get, this score is taken from calculations done to data in a database on the request of the user. I have only 1 table in that database and a few columns (user,age,genre,language,format,…etc).
What I want to do is, once the user clicks “find match” button on the chatbot, this user’s data, which is already in the database will be compared to the other user’s data in the same table and compare each column 1 by 1 of each row.
For example, the user’s genre preference will be compared to each genre pref of the other users in each row of the table, when there is a match, 1 point is added, then language will be compared of each user and 1 point is given when there’s a match. This will go to each column in each row and be compared with the user’s. In the end, the users that has highest matching points will be recommended to this user.
What’s the best way and approach to do that? I am using nodejs and mysql database.
Thank you.
Advertisement
Answer
I see this as a self join and conditional expressions:
select t.*, (t1.genre = t.genre) + (t1.language = t.language) + (t1.format = t.format) as score from mytable t inner join mytable t1 on t1.user <> t.user where t1.user = ? order by score desc
The question mark represents the id of the currently logged on user, for who you want to search matching users. The query brings all other users, and counts how many values they have in common over the table columns: each matching value increases the score by 1
. Results are sorted by descending score.