본문 바로가기

SELF JOIN3

[LeetCode] Consecutive Numbers (self join twice) # Write your MySQL query statement below SELECT DISTINCT l1.Num as ConsecutiveNums FROM Logs l1 JOIN Logs l2 ON l1.Id = l2.Id - 1 JOIN Logs l3 ON l2.Id = l3.Id - 1 WHERE l1.Num = l2.Num AND l2.Num = l3.Num ; self join 을 2번한다. 총 3개의 table이 join한다 결과 {"headers": ["Id", "Num", "Id", "Num", "Id", "Num"], "values": [[1, 1, 2, 1, 3, 1], [2, 1, 3, 1, 4, 2], [3, 1, 4, 2, 5, 1], [4, 2, 5, 1, 6, 2], [5, 1.. 2021. 4. 10.
[LeetCode] Rising Temperature (interval, self join) 데이터 타입이 date라는 점을 주목하자 Select w1.id from weather w1 join weather w2 on w1.recorddate = date_add(w2.recorddate, interval +1 day) where w1.temperature > w2.temperature date_add() Add n days to a date and return the date select *로 출력해보면 {"headers": ["Id", "RecordDate", "Temperature", "Id", "RecordDate", "Temperature"], "values": [[2, "2015-01-02", 25, 1, "2015-01-01", 10], [3, "2015-01-03", 20, 2, .. 2021. 4. 9.
[LeetCode] Employees Earning More Than Their Managers (Self Join) select e.Name as Employee from Employee as e join Employee as m on m.Id = e.ManagerId where e.Salary > m.Salary 위 코드에서 e는 Joe, Henry 만 남아있고(join의 결과 managerID == ID) m은 Sam, Max만 존재하는 것이다. self join www.w3schools.com/sql/sql_join_self.asp SQL Self Join SQL Self Join SQL Self Join A self join is a regular join, but the table is joined with itself. Self Join Syntax SELECT column_name(s) FROM table.. 2021. 4. 8.