Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

craigwillis's avatar

Many-to-many and grouping

Hi

I have 4 tables:

groups:
id, name

group_members:
id, group_id, user_id

users:
id

notifications
id, user_id, notification

What I'm looking to do, is to get all of the notifications that a user has created, but they must be a member of an organization.

So, for example I want to get all the notifications, that are made by users that are members of group 1

groups
1, test
2, test-1
3, test-2

group_members
1, 1, 1
2, 1, 2
3, 3, 1

users
1
2
3

notifications
1, 1, Notification One
2, 1, Notification Two
3, 3, Notification Three
4, 3, Notification Four
5, 2, Notification Five

The above should return me:

notifications
1, 1, Notification One
2, 1, Notification Two
5, 2, Notification Five

Is this possible in a single query, or am i likely to need multiple queries to get what I'm looking for?

Thanks

0 likes
0 replies

Please or to participate in this conversation.