Skip to content

Instantly share code, notes, and snippets.

@jeffrafter
Created July 28, 2008 08:57
Show Gist options
  • Save jeffrafter/2864 to your computer and use it in GitHub Desktop.
Save jeffrafter/2864 to your computer and use it in GitHub Desktop.
I want to JOIN only the last item
table_1
------------
id | Name |
============
1 Bob |
============
table_2
---------------------------
id | table_1_id | height |
===========================
1 1 191 |
2 1 193 |
===========================
SELECT * FROM table_1
INNER JOIN table_2 ON table_1.id = table_2.table_1_id
Result:
=================================
1 Bob 1 1 191 |
1 Bob 2 1 193 |
=================================
What I want:
=================================
1 Bob 2 1 193 |
=================================
What I came up with:
SELECT * FROM table_1
INNER JOIN (SELECT * from table_2 ORDER BY id DESC LIMIT 1) as table_2 ON table_1.id = table_2.table_1_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment