Test data: mysql> select * From T2; + ---- + -------- + --------------------- + ---------- + | ID | userid | inputdate | infostatus | + ---- + -------- + --------------------- + ---------- + | 1 | 1 | 00:00:00 | 20013 | 2 | 1 | 00:00:00 | 0 | 3 | 2 | 00:00:11 | 20015 | 4 | 2 | 00:00:22 | 20013 | 5 | 2 | 00:00:00 | 0 | 6 | 2 | 00:00:00 | 20013 | 7 | 2 | 00:00:00 | 0 | 8 | 2 | 00:00:00 | 0 | 9 | 2 | 00:00:00 | 20013 | 10 | 2 | 00:00:00 | 0 | 11 | 2 | 00:00:00 | 0 | + ---- + -------- + ----------------------- + ------------ + query SQL: select ID, userid, date (inputdate) as date, max (infostatus) as M, min (infostatus) As I, if (min (infostatus) = 0, max (infostatus )) as Max from T2 where infostatus = 0 or infostatus <= 99999 group by date; receipt result: + ---- + -------- + ------------ + ------- + | ID | userid | date | M | I | max | + ---- + -------- + ------------ + ------- + | 1 | 1 | 2014-07-11 | 20013 | 0 | 0 | 3 | 2 | 2014-07-12 | 20015 | 20013 | 20015 | 9 | 2 | 20013 | 0 | 0 | 5 | 2 | 2014-07-14 | 0 | 0 | 0 | 6 | 2 | 2014-07-15 | 20013 | 0 | 0 | 8 | 2 | 2014-07-16 | 0 | 0 | 0 | + ---- + -------- + ------------ + ------- + my idea is to group by time, at the same time, the query is the smallest, followed by the largest. If the smallest is 0, then the max field is 0; otherwise, the largest one is obtained.