[mySQL] 리트코드 Rising Temperature

sehyunny·2023년 4월 21일
0

mySQL

목록 보기
7/26

https://leetcode.com/problems/rising-temperature/

Write an SQL query to find all dates' Id with higher temperatures compared to its previous dates (yesterday).
Return the result table in any order.
The query result format is in the following example.

Output:
+----+
| id |
+----+
| 2 |
| 4 |
+----+

조회할 데이터 : 전 날보다 기온이 높은 날들의 id

SELECT Today.id
FROM weather AS Today
     LEFT JOIN weather AS Yesterday ON Today.recorddate = DATE_ADD(Yesterday.recorddate, INTERVAL 1 DAY)
WHERE Today.temperature > Yesterday.temperature

☑️ point

- mySQL에서 시간 더하기, 빼기 DATE_ADD, DATE_SUB
DATE_ADD(기준, INTERVAL ~ SECOND)
DATE_SUB(기준, INTERVAL ~ SECOND)
SECOND 대신 MINUTE, HOUR, DAY, YEAR 모두 가능하다

- DATE_ADD(기준, INTERVAL ~ - YEAR)
= DATE_SUB(기준, INTERVAL ~ YEAR)

0개의 댓글