简体   繁体   English

在多对多连接表中,如何计算两个“所有者”共享的条目数?

[英]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.

相关问题 如何加入左表可以为空或右表可以为空的多对多? - How do I join a many-to-many where the left table can be null OR the right table can be null? 如何优化具有多对多关系的 mysql COUNT() 查询(通过第 3 个表) - How can i optimize the mysql COUNT() query with many-to-many relationship(through the 3rd table) 如何引用多对多关系的相交表? - How can I reference an intersection table of a many-to-many relationship? 多对多联接表可以有两列以上吗? - Can a many-to-many join table have more than two columns? Laravel:多对多共享表 - Laravel: many-to-many with shared table 以一对多的两个表,如何过滤多表,然后将所有匹配项加入1表? - Taking two tables, 1-to-many, how can I filter down the many table and then join ALL the matches in the 1 table? 教义多对多联接表未填充 - Doctrine many-to-many join table not populating 多对多联接表的foreignKey或inverseForeignKey - foreignKey or inverseForeignKey for many-to-many join table 如何使用NotORM联接多对多表 - How do I JOIN many-to-many tables using NotORM 如何通过对同一关系进行过滤来查询多对多联接? - How can I query a many-to-many join with filtering on the same relation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM