I have 4 tables, and the goal is a simple true/false (or count if it's easier) for whether there exists either an AccountLink or a ResourceLink based on given User.uuid and Resource.uuid.
Minimal working example
http://sqlfiddle.com/#!17/09144/1
Goal
True/false response based on User.uuid and Resource.uuid, which are unrelated.
Schema
User
- id
- uuid
Resource
- id
- account_id
- uuid
AccountLink
- id
- account_id
- user_id
ResourceLink
- id
- resource_id
- user_id
Currently working
Gives id list instead of true/false, which can be handled in application code, but the query itself is also inefficient:
SELECT id
FROM "AccountLinks"
WHERE user_id = (SELECT id FROM "Users" WHERE uuid = ?)
AND account_id = (SELECT account_id FROM "Resources" WHERE uuid = ?)
UNION
SELECT id
FROM "ResourceLinks"
WHERE user_id = (SELECT id FROM "Users" WHERE uuid = ?)
AND resource_id = (SELECT id FROM "Resources" WHERE uuid = ?)
Is there a way to find the correct user and resource once based on their uuid, and then use those id properties to count across multiple tables?
user.uuidrelated toresource.uuid? or are two separated inputs?