Compare similarities between two result sets

前端 未结 2 1227
广开言路
广开言路 2021-01-20 11:49

I am creating a music website where I would like users to be able to find users who like approximately the same artists as they.

I have a \'like\' table that has 2 c

2条回答
  •  一整个雨季
    2021-01-20 12:04

    It is possible to join a table to itself. (You need to specify an alias for at least one of the two "copies" of the table, so that your query is not ambiguous.)

    So given two users, you can find the "likes" they have in common by doing a join of the like table to itself. You can also find what proportion of User 1's likes are shared by User 2 by doing a left join and counting both how many results there are and how many are null. Note that this is not a symmetric operation, and you will need to tackle the case where one or both of the numbers is 0.

    When you say you want to "find the most similar people in the database": you could do this for every pair of users, but note that if you have n users then this involves doing n*(n-1)/2 comparisons, which is on the order of n squared. This might be quite a lot of work for your database to do if you have a lot of users.

提交回复
热议问题