Most efficient way to select events from users having common subscriptions - Mailing list pgsql-sql

From Brice André
Subject Most efficient way to select events from users having common subscriptions
Date
Msg-id CAOBG12mB0Vhx11xq73K2Yxjxg35Ayw9vSzsz-Hv06xNJGgXbSA@mail.gmail.com
Whole thread Raw
List pgsql-sql
Hello,

I have a project on which users may be registered to different challenges. And I would want to implement a "news" panel, where users are notified of events performed by other users, which have at least one challenge in common.

My DB schema has tables like follows :
  • table "user"
    • user_id
    • etc.
  • table "challenge"
    • challenge_id
    • etc.
  • table "user_challenge_association"
    • user_id
    • challenge_id
    • team_id
  • table "activity"
    • user_id
I would thus want to perform a request returning all activities of all users which have at least one entry in "user_challenge_association" where "challenge_id" is the same as another entry in "user_challenge_association" associated to the user for which I am doing the request.

Imagine "user" contains (user_1, user_2, user_3), imaging "challenge" contains (challenge_1, challenge_2), imagine that user_1 is only registered to challenge_1 user_2 is only registered to challenge_2, and user_3 is registered to both challenges, I would want :
  • that request for user_1 only returns activities of user_3
  • that request for user_2 only returns activities of user_3
  • that request for user_3 returns activities of user_1 and user_2
What would be the best way to perform such a query ?

Additional question : How could I integrate in the result a column indicating if, in all existing challenges, the resulting activity is linked to a user that was at least once in the same team as the user for which we perform the request ? (team information is in "user_challenge_association" table

Many thanks for your advices,
Brice

pgsql-sql by date:

Previous
From: Zainik Theme
Date:
Subject: #1 Best Open Source eCommerce Platform of 2022
Next
From: "Voillequin, Jean-Marc"
Date:
Subject: execution id