Get list of last record for each ID in SQL -


i last records every id_foreign in sql, example want do:

input:

id: 1, id_foreign: 5, value: 1  id: 2, id_foreign: 4, value: 2  id: 3, id_foreign: 4, value: 3  id: 4, id_foreign: 5, value: 4  id: 5, id_foreign: 5, value: 5  

output:

id: 3, id_foreign: 4, value: 3  id: 5, id_foreign: 5, value: 5 

what distinct? other idea? understand me?

tried solution:

select * table  group id_foreign  order id desc 

but outputs me:

id: 1, id_foreign: 5, value: 1  id: 2, id_foreign: 4, value: 2  

i tried both (asc , desc) , bad output too.

a simple not optimal way of querying using not exists:

select * mytable t not exists (     select * mytable tt tt.id_foreign=t.id_foreign , tt.id > t.id ) 

demo on sqlfiddle.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -