본문 바로가기

SQL/MySql

Mysql MAX 최신 날짜로 조회하기 INNER JOIN 사용

최근날짜의 데이터만 불러오고 싶을 때, 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

 

[MySQL] 날짜 기준으로 최신 행 가져오기 (중복 없이)

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 예제는 username으로 groupby하여 중복을 제거하고, 조

palbok.tistory.com

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