select c.user_id, c.room_id, g.room_type, datediff(c.checkout_time,c.checkin_time) as days from checkin_tb c join guestroom_tb g on g.room_id=c.room_id where datediff(date_format(c.checkout_time,'%Y-%m-%d'),date_format(c.checkin_time,'%Y-%m-%d'))>1 order by days asc,c.room_id asc,c.user_id desc; ...