mysql - Order by desc and group by on cross table query -


i've tried few options i've seen getting group , order work together...but can't right results.

feeding_types table     id                               type                                 quantity                                                 feeding_id  feedings table     id                                                           data_entry_id 

the basic part of query returns following:

        id      type        feeding_id         15236   dried_comm  13499         15237   dried_comm  13500         15286   dried_comm  13543         15287   dried_comm  13544         15294   tinned_comm 13550         15295   dried_comm  13551         15296   dried_comm  13552 

what want use group latest dried_comm , tinned_comm records these results, , filter out rest.

i've tried this:

select ft . * , max( ft.id ) id feeding_types ft inner join feeding_types ft2 on ft2.id = ft.id join feedings f1 on ft.feeding_id = f1.id f1.data_entry_id = 15758 group ft.type order id desc 

which results in:

id      type       feeding_id   id  15236   dried_comm  13499   15296 15294   tinned_comm 13550   15294 

edit: expected results id type feeding_id id 15296 dried_comm 13552 15296 15294 tinned_comm 13550 15294

any ideas how working correctly?

the underlying logic not match actual algorithm. let me suggest following instead:

select ft.* feeding_types ft     join (         select type, max(id) id          feeding_types          group type     ) t on t.type = ft.type , t.id = ft.id     join feedings f1 on ft.feeding_id = f1.id , f1.data_entry_id = 15758 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -