English 中文(简体)
互访地点
原标题:SQL query for mutual visited places

我为我国有铁路3/PostgreSQL的大学开展了一个项目,在那里我们有用户、活动和地点。 用户开展了许多活动,一个场所开展了许多活动。 一项活动属于用户和场所,因此具有用户资格和地点。

我需要的是寻找几个用户之间的共同点的“结构”(或甚至铁路本身的一种方法?)。 例如,我有5个用户访问了不同地点。 5个用户只访问了2个场所。 因此,我想收回两个地点。

I ve started by retrieving all activities from the 5 users:

SELECT a.user_id as user, a.venue_id as venue
FROM activities AS a
WHERE a.user_id=116 OR a.user_id=227 OR a.user_id=229 OR a.user_id=613 OR a.user_id=879

But now I need a way to find out the mutual venues. Any idea?

thx, tux

最佳回答

I m not entirely familiar with sql syntax for postgresql, but try this:

select venue_id, COUNT(distinct user_id) from activities Where user_id in (116,227,229,613,879) group by venue_id having COUNT(distinct user_id) = 5

EDIT:

你们需要把5至5个用户改换成你们所关心的用户(你们当中有多少人正在寻找)。

我在表层结构中测试了这一情况:

user_id     venue_id    id
----------- ----------- -----------
1           1           1
2           6           2
3           3           3
4           4           4
5           5           5
1           2           6
2           2           7
3           2           8
4           2           9
5           2           10

产出如下:

venue_id    
----------- -----------
2           5
问题回答

你必须提出一些参数,供你搜寻。 例如,5个用户可能拥有2个共同点,但不是3个。

如果你想要看到这五个用户的共同点,你可以开始这样做:

SELECT a.venue_id, count(1) as NoOfUsers
FROM activities AS a
WHERE a.user_id=116 OR a.user_id=227 OR a.user_id=229 OR a.user_id=613 OR a.user_id=879
group by a.venue_id

对于这些用户来说,这将给你们带来多少用户拥有这一场所。 因此,你拥有“Venue分享”的程度。

但是,如果你想看到五位用户访问的会址,你会最终增加一条线:

SELECT a.venue_id, count(1) as NoOfUsers
FROM activities AS a
WHERE a.user_id=116 OR a.user_id=227 OR a.user_id=229 OR a.user_id=613 OR a.user_id=879
group by a.venue_id
having count(1) = 5 --the number of users in the query

您也应考虑修改<条码>。 WHERE

WHERE a.user_id=116 OR a.user_id=227 OR a.user_id=229 OR a.user_id=613 OR a.user_id=879

纽约总部

WHERE a.user_id in (116, 227, 229, 613, 879)

简言之,它就象:

Select distinct v.venue_id
from v.venues
join activities a on a.venue_id = v.venue_id
Join users u on u.user_id = a.user_id
Where user_id in (116,227,229,613,879)

您需要加入您的表格,以便找到所有有用户活动的地点。 当你刚刚学习时,如果你使用分局,有时会更简单地视而不见。 至少是我对我的看法。





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签