I am having 5 tables users,,groups user_groups, documents and document_groups table. I want to fetch all documents that belongs to users with their groups
Users Table
id name email
1 John [email protected]
2 Martin [email protected]
3 Kane [email protected]
Groups Table
id group_name
1 Accounts Group
2 IT Group
User Groups Table
user_id group_id
1 1
2 1
3 2
John and martin belongs to Accounts Group and Kane belongs to IT GROUP
Documents Table
id document_name share_type
1 Policy Document all
2 Personal Document group
Now Fisrt document will be visible to all users in database but 2nd document should be accessible to some groups may be 1 or more then 1.
Now we have last table
Document Groups
id group_id document_id
1 1 2
Now 2nd document should only accessible to Account Groups(Group id :1) and in Account Groups only have 2 users John and Martin
I have tried below code
Select * from documents where share_type ="all" OR
So my final outupt if i logged in as User John
document_id document_name
1 Policy Document
2 Personal Document
So my final outupt if i logged in as User Kane
document_id document_name
1 Policy Document
New contributor
user is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.