select user_id, room_id, room_type, days from ( select user_id, b.room_id, room_type, datediff(checkout_time, checkin_time) as days from guestroom_tb a join checkin_tb b on a.room_id=b.room_id ) temp where days>1 order by days, room_id, user_id desc