给定两个相关表,如何确定最常见的关系?
Given two related tables, how to determine the most common relationships?
给定 3 个表:用户、书籍、book_users,我如何确定哪些是公共书籍?
users: id, first_name, last_name
books: id, name
books_users: book_id, user_id
设计器输出,类似于:
book | count
radBookName | 22
SemiRad | 22
谢谢
您似乎想要带有 GROUP BY
子句的简单 JOIN
:
SELECT b.name, count(*) as user_count
FROM books b INNER JOIN
books_users bu
ON bu.book_id = b.id
GROUP BY b.name;
如果一本书有相同的用户,这将产生重复计数,如果您想要唯一计数,请改用 count(distinct bu.user_id)
。
给定 3 个表:用户、书籍、book_users,我如何确定哪些是公共书籍?
users: id, first_name, last_name
books: id, name
books_users: book_id, user_id
设计器输出,类似于:
book | count
radBookName | 22
SemiRad | 22
谢谢
您似乎想要带有 GROUP BY
子句的简单 JOIN
:
SELECT b.name, count(*) as user_count
FROM books b INNER JOIN
books_users bu
ON bu.book_id = b.id
GROUP BY b.name;
如果一本书有相同的用户,这将产生重复计数,如果您想要唯一计数,请改用 count(distinct bu.user_id)
。