[英]In a many-to-many join table, how can I count the number of entries shared by two “owners”?
I have a list of movies and a list of tropes.我有一个电影列表和一个比喻列表。 To calculate the similarity between two movies, I am using cosine differences .
为了计算两部电影之间的相似度,我使用了余弦差异。 If all the weights are even, then it simplifies pretty well:
如果所有的权重都是偶数,那么它就可以很好地简化:
similarity =
(number of shared tropes between both movies)
/
(SQRT(number of tropes from movie 1) + SQRT(number of tropes from movie 2))
For example, if movie 1 has tropes 1, 3, and 4, and movie 2 has tropes 1, 4, 6, and 7, then there would be two tropes shared between them and the similarity would be例如,如果电影 1 有 1、3 和 4,而电影 2 有 1、4、6 和 7,那么它们之间将共享两个比喻,相似度为
2 / (SQRT(3) + SQRT(4)) = 2 / 3.73... = 0.54
My MySQL tables are pretty standard:我的 MySQL 表非常标准:
movies:
- id
- ...
tropes:
- id
- ...
movie_tropes:
- movie_id
- trope_id
I can easily count the number of tropes for a movie:我可以很容易地计算一部电影的比喻数量:
SELECT count(distinct trope_id) from movie_tropes where movie_id = 1;
SELECT count(distinct trope_id) from movie_tropes where movie_id = 2;
I am a little out of practice with SQL.我对 SQL 有点不习惯。 Is there a simple join-y way to count the number of trope_ids that occur for both movie 1 and movie 2 in this join table?
是否有一种简单的连接方式来计算此连接表中电影 1 和电影 2 出现的 trope_id 数量?
Is there a simple way to count the number of trope_ids that occur for both movie 1 and movie 2?
有没有一种简单的方法来计算电影 1 和电影 2 出现的 trope_id 数量?
You can self-join:您可以自行加入:
select count(distinct trope_id)
from movie_tropes t1
inner join movie_tropes t2 on t2.trope_id = t1.trope_id
where t1.movie_id = 1 and t2.movie_id = 2
But overall, you can compute the three base figures at once with two levels of aggregation.但总的来说,您可以使用两个聚合级别一次计算三个基数。 I would recommend:
我会推荐:
select
sum(has_1) as cnt_1, -- count of distinct tropes for movie 1
sum(has_2) as cnt_2, -- count of distinct tropes for movie 2
sum(has_1 and has_2) as cnt_both -- count of distinct tropes for both movies
from (
select max(movie_id = 1) has_1, max(movie_id = 2) as has_2
from movie_tropes t
where movie_id in (1, 2)
group by trope_id
) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.