Skip to content

Instantly share code, notes, and snippets.

@skrosoft
Created February 15, 2017 19:36
Show Gist options
  • Save skrosoft/035a7755a94ee5bf3ee60593f5005f02 to your computer and use it in GitHub Desktop.
Save skrosoft/035a7755a94ee5bf3ee60593f5005f02 to your computer and use it in GitHub Desktop.
limit_left_join_to_one_result.sql
// Seen on http://stackoverflow.com/questions/2978726/limit-results-from-joined-table-to-one-row
SELECT * FROM days_off_request AS dor
INNER JOIN days_off_request_status_history AS dorsh
ON dor.id=dorsh.days_off_request
LEFT JOIN days_off_request_status_history AS dorsh2
ON dor.id=dorsh2.days_off_request
AND dorsh2.created_at>dorsh.created_at
WHERE dorsh2.created_at IS NULL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment