Skip to content

Instantly share code, notes, and snippets.

Created July 30, 2014 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ef30a9c590bf51c0bff7 to your computer and use it in GitHub Desktop.
Save anonymous/ef30a9c590bf51c0bff7 to your computer and use it in GitHub Desktop.
clocks: shift_id
shifts: employee_id
employee: name
hierarchies: inferior_id, superior_id (both link to employee)
Want to only return clocks whose associated employee has a hierarchy
where their id matches the inferior_id and the superior_id matches 4
or the associated employee's id is 4
SELECT "clocks".* FROM "clocks"
INNER JOIN "shifts" ON "shifts"."id" = "clocks"."shift_id"
INNER JOIN "employees" ON "employees"."id" = "shifts"."employee_id"
INNER JOIN "hierarchies" ON "hierarchies"."inferior_id" = "employees"."id"
INNER JOIN "employees" "superiors_employees" ON "superiors_employees"."id" = "hierarchies"."superior_id"
WHERE (hierarchies.superior_id = 3 OR employees.id = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment