I have the following three tables:
templates <-----> templatesubscription <-----> user
table template:
id | name
table user
id | name | ...
table templatesubscriptions
id | template_id | user_id
- Each user can have n template subscriptions.
- Each template subscription can have 1 user.
- Each template has n template subscriptions.
- Each template subscription has 1 template.
Now I want to check if the currently logged in user (User ID = 10) can edit the template with the ID = 0. Only if the user has an template subscription for this template he can edit it.
Now I could make 2 queries. First I will get the template subscription of the current user with the template id:
SELECT * FROM templatesubscription WHERE user_id = 10 AND template_id = 0
Now I can check if the return value is null, if yes the user will get an error message. If the result isn't empty, I will get the template of the template table:
SELECT * FROM templates WHERE id = 0
But this approach doesn't seems really nice to me. Can I combine both statements in one querie? The return value should be the template or null if the user has no template subscription for this template.