Skip to main content
edited tags
Link
Hannah Vernon
  • 70.5k
  • 22
  • 177
  • 321
Tweeted twitter.com/#!/StackDBAs/status/519563846852165634
Source Link

Finding records not present across 3 tables - Sqlite3

From the diagram provided, what I'm trying to achieve is to get all the Users of a particular workspace that are NOT associated with a task within user_task.

ERD diagram

user_workspace table contains two FKs: user_id (from User) and workspace_id (from Workspace)

user_task table contains two FKs: user_id (From user) and task_id (from Task)

Here's the SQL I'm tinkering with to give a sense of what I'm trying to achieve:

SELECT * FROM User INNER JOIN user_workspace ON (user_workspace.user_id = User._id) 
LEFT OUTER JOIN user_task ON (user_task.task_id = Task._id) 
WHERE (user_workspace.workspace_id= ?) 
AND (user_task.task_id= ? AND user_task.user_id IS NULL)
ORDER BY User.name ASC

I think I'm close but I can't seem to get the exact result I want. Thanks for reading.