Skip to content

Instantly share code, notes, and snippets.

@scwood
Last active January 13, 2018 00:20
Show Gist options
  • Select an option

  • Save scwood/204882ae9d94818c0d5c7383dbaa18ab to your computer and use it in GitHub Desktop.

Select an option

Save scwood/204882ae9d94818c0d5c7383dbaa18ab to your computer and use it in GitHub Desktop.
Join one to many relationship, select one row per the one on some criteria
-- one customer to many transactions
-- want to select most recent transaction for each customer
select * from customers join transactions on transactions.id = (
select id from transactions
where transactions.customer_id = customers.id
order by created_at desc
limit 1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment