데이터베이스 - MySQL 기본문법(JOIN)

이우건·2023년 2월 7일

SQL 

목록 보기
7/9
  • 다음과 같은 2개의 관계형 table이 있다.
    topic table

    author table

  • topic 테이블의 author_id와 author 테이블의 id는 서로 연관이 되어있는 것을 알 수 있다. 이를 하나로 묶어서 나타내주려면 JOIN문을 사용한다.

->는 enter를 의미함

SELECT * FROM topic
    -> LEFT JOIN author
    -> ON topic.author_id = author.id;
  • LEFT JOIN 키워드는 LEFT JOIN 키워드는 왼쪽 테이블(table1)의 모든 레코드와 오른쪽 테이블(table2)의 일치하는 레코드를 반환한다.
  • author_id와 id는 겹치는 부분이므로 author 테이블의 id를 제외하고 출력해보자.
->는 enter를 의미함

SELECT topic.id, title, description, created, name, profile FROM topic
    -> LEFT JOIN author
    -> ON topic.author_id = author.id;

  • topic 테이블의 id 이름을 변경하여 출력하고 싶을 때는 AS 키워드를 사용한다.
SELECT topic.id AS topic_id, title, description, created, name, profile FROM topic
    -> LEFT JOIN author
    -> ON topic.author_id = author.id;

profile
머리가 나쁘면 기록이라도 잘하자

0개의 댓글