Created
September 14, 2015 20:10
-
-
Save bbatha/f4fd16b5316563f636b5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
create_table(:minors) do | |
primary_key :id | |
end | |
create_table(:states) do | |
primary_key :id | |
foreign_key :minor_id, :minors | |
String :state, :size => 10 | |
end | |
SELECT minors.*, sm.state | |
FROM MINORS | |
INNER JOIN ( | |
SELECT | |
states.id, states.state, states.minor_id | |
FROM | |
states | |
LEFT OUTER JOIN states AS maxstates | |
ON | |
maxstates.minor_id = states.minor_id | |
AND maxstates.id > states.id | |
) sm ON sm.minor_id = minors.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment