코딩테스트 연습 > String, Date > 오랜 기간 보호한 동물(2)
https://school.programmers.co.kr/learn/courses/30/lessons/59411


animal_outs와 animal_ins에 모두 datetime 기록이 있어야 하므로, inner join을 통해 두 테이블을 join 한다.
이 후, timestampdiff를 통해 second 단위로 datetime의 차를 구한 뒤, 내림차순으로 정렬한다.
정렬된 결과 바탕으로 상위 2개를 추출하여 animal_id, name을 select 한다.
select ai.animal_id, ai.name
from animal_ins as ai
inner join animal_outs as ao
on ai.animal_id = ao.animal_id
order by timestampdiff(second, ai.datetime, ao.datetime) desc
limit 2;
timediff, datediff는 각각 시,분,초 / 일 수 만 계산하므로, timestampdiff를 second 단위로 계산해야 정확한 차이를 알 수 있다.