Skip to content

Instantly share code, notes, and snippets.

Created July 30, 2014 07:22
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/04d8539f2dd7a3d27156 to your computer and use it in GitHub Desktop.
Save anonymous/04d8539f2dd7a3d27156 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
WHERE (hierarchies.superior_id = 4 OR employees.id = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment