SQL/MySql
Mysql MAX 최신 날짜로 조회하기 INNER JOIN 사용
daeyun대윤
2023. 3. 12. 08:15
최근날짜의 데이터만 불러오고 싶을 때, WHERE 절 말고 INNER JOIN으로
select s.CNTNG_DT, s.SCN_ID, s.VHCL_ID, s.VHCL_ALS, s.TRMNL_ID
from s_vhcl_als_day AS s
inner join (
SELECT SCN_ID, MAX(CNTNG_DT) as MaxDate FROM s_vhcl_als_day
WHERE SCN_ID = "S000000062"
) AS b
on s.CNTNG_DT = b.MaxDate
AND s.SCN_ID = b.SCN_ID
ORDER BY VHCL_ID;
참고 글 : https://palbok.tistory.com/58
select t.username, t.date, t.value
from MyTable t
inner join (
select username, max(date) as MaxDate
from MyTable
group by username
) tm on t.username = tm.username and t.date = tm.MaxDate
728x90
300x250