saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 알고리즘문제해결
  • 취업준비
  • 파이썬
  • 딥러닝
  • 릿코드
  • BFS
  • 문제해결능력
  • 개발자 취업준비
  • Python
  • c++
  • DFS
  • 알고리즘
  • two pointer
  • 딕셔너리
  • 백준
  • 온라인저지
  • LeetCode
  • 취준
  • 리트코드
  • 개발자

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeedCode] 175. Combine Two Tables SQL Easy

2021. 11. 3. 05:16

문제 설명:

테이블이 두개가 있다.
Person 테이블과 Address 테이블이 있는데, personId를 기준으로 
주소를 연결해줘야한다. 만약 주소가 존재하지 않으면 null로 표기한다.

문제 풀이:

1. left join으로 Address 테이블을 Person 테이블에 연결한다. 
2. 그리고 Person테이블의 PersonId 와 Address테이블의 PersonId를 조건으로 달아준다.

Table: Person+-------------+---------+ | Column Name | Type | +-------------+---------+ | personId | int | | lastName | varchar | | firstName | varchar | +-------------+---------+ personId is the primary key column for this table. This table contains information about the ID of some persons and their first and last names.

 

Table: Address

+-------------+---------+ | Column Name | Type | +-------------+---------+ | addressId | int | | personId | int | | city | varchar | | state | varchar | +-------------+---------+ addressId is the primary key column for this table. Each row of this table contains information about the city and state of one person with ID = PersonId.

 

Write an SQL query to report the first name, last name, city, and state of each person in the Person table. If the address of a personId is not present in the Address table, report null instead.

Return the result table in any order.

The query result format is in the following example.

 

Example 1:

Input: Person table: +----------+----------+-----------+ | personId | lastName | firstName | +----------+----------+-----------+ | 1 | Wang | Allen | | 2 | Alice | Bob | +----------+----------+-----------+ Address table: +-----------+----------+---------------+------------+ | addressId | personId | city | state | +-----------+----------+---------------+------------+ | 1 | 2 | New York City | New York | | 2 | 3 | Leetcode | California | +-----------+----------+---------------+------------+ Output: +-----------+----------+---------------+----------+ | firstName | lastName | city | state | +-----------+----------+---------------+----------+ | Allen | Wang | Null | Null | | Bob | Alice | New York City | New York | +-----------+----------+---------------+----------+ Explanation: There is no address in the address table for the personId = 1 so we return null in their city and state. addressId = 1 contains information about the address of personId = 2.

소스코드:

# Write your MySQL query statement below
# 선택하는 컬럼의 순서는 상관 없다.
select FirstName, LastName, City, State
# 테이블을 연결하는 위치가 중요하다. right join의 경우 어드레스 부터 저장된다.
# 그리고 join만 사용했을 경우 null로 표기된 row 가 합쳐지지 않는다.
from Person left join Address
# 조건은 아이디로 통일한다.
on Person.PersonId = Address.PersonId;

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeedCode]819. Most Common Word 파이썬 Easy  (0) 2021.11.04
[LeedCode] 53. Maximum Subarray 파이썬 Easy  (0) 2021.11.04
[LeedCode] 423. Reconstruct Original Digits from English 파이썬 Medium  (0) 2021.11.03
[LeetCode] 3. Longest Substring Without Repeating Characters 파이썬 Medium  (0) 2021.11.02
[LeedCode] 1041. Robot Bounded In Circle 파이썬 Medium  (0) 2021.11.02
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeedCode]819. Most Common Word 파이썬 Easy
    • [LeedCode] 53. Maximum Subarray 파이썬 Easy
    • [LeedCode] 423. Reconstruct Original Digits from English 파이썬 Medium
    • [LeetCode] 3. Longest Substring Without Repeating Characters 파이썬 Medium
    saurus2
    saurus2
    Simple is Best

    티스토리툴바