Last active
August 29, 2015 14:05
-
-
Save kballenegger/7e1e1498bab1a50eac8c to your computer and use it in GitHub Desktop.
Postgres select by most recently used in relation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT payees.*, max(payments.id) AS most_recent_transaction FROM payees | |
LEFT OUTER JOIN payments ON payments.payee_id = payees.id | |
WHERE payees.user_id = ? | |
GROUP BY payees.id | |
ORDER BY max(payments.id) DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My error: (with different table names)