Select only last value using group by at mysql

后端 未结 6 699
有刺的猬
有刺的猬 2020-12-11 15:16

I have one table with data about attendance into some events. I have in the table the data of the attendance everytime the user sends new attendance, the information is like

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 15:30

    Here is one option (untested):

    SELECT v.id_branch_channel, v.id_member, v.attendance, v.timestamp, v.id_member 
    FROM view_event_attendance v
        JOIN (
            SELECT id_event, id_member, MAX(attendance) maxattendance
            FROM view_event_attendance 
            GROUP BY id_event, id_member ) m ON 
                v.id_event = m.id_event AND
                v.id_member = m.id_member AND
                v.attendance = m.maxattendance
    WHERE v.id_event = 782 
    GROUP BY v.id_member;
    

    The concept is to get the MAX() of timestamp and use that field to JOIN on your view. You might not need all the fields -- really depends on your table structure. But this should get you going in the correct direction.

提交回复
热议问题