how to get distinct rows with max value

前端 未结 4 1727
萌比男神i
萌比男神i 2020-12-17 04:47

my apologies for asking what must be very simple to solve, but I just can\'t seem to wrap my mind around this.. I couldn\'t even come up with a really fitting title for my q

4条回答
  •  感动是毒
    2020-12-17 05:48

    You can use a sub-select:

    select min(a1.id), a1.authorid, a2.mxvotes
    from answers a1
    inner join
    (
      select authorid, max(votes) mxvotes
      from answers
      group by authorid
    ) a2
      on a1.authorid = a2.authorid
      and a1.votes = a2.mxvotes
    group by a1.authorid, a2.mxvotes
    order by mxvotes desc
    

    see SQL Fiddle with Demo

提交回复
热议问题