[8주차] SQL

xktm-woonge·2023년 6월 23일
post-thumbnail

UNION

여러 개의 SQL문을 합쳐서 하나의 SQL문으로 만들어주는 방법(칼럼의 개수가 같아야함)

문법

  • UNION : 중복된 값을 제거하여 알려준다.
  • UNION ALL : 중복된 값도 포함하여 모두 보여준다.
    select column from table UNION select column from table1

JOIN

INNER JOIN

두 개의 테이블에서 공통된 요소들을 통해 결합하는 방식(교집합)

select column
from table1
INNER JOIN table2
ON table1 column = table2 column
where 조건;

LEFT JOIN

두 개의 테이블에서 공통영역을 포함해 왼쪽 테이블의 다른 데이터를 포함하는 방식

select column
from table1
LEFT JOIN table2
ON table1.column = table2.column
where 조건

RIGHT JOIN

두 개의 테이블에서 공통영역을 포함해 오른쪽 테이블의 다른 데이터를 포함하는 방식

select column
from table1
RIGHT JOIN table2
ON table1.column = table2.column
where 조건

FULL OUTER JOIN

두 개의 테이블에서 공통영역을 포함해 양쪽 테이블의 다른 영역을 모두 포함하는 방식(합집합)

select column
from table1
FULL OUTER JOIN table2
ON table1.column = table2.column
where 조건

MySQL에서는 FULL JOIN을 지원하지 않는다.

select column
from table1
LEFT JOIN table2
ON table1.column = table2.column
where 조건
UNION
select column
from table1
RIGHT JOIN table2
ON table1.column = table2.column
where 조건

SELF JOIN

select column
from table1, table2
where 조건

CONCAT

여러 문자열을 하나로 합치거나 연결

select concat(str1,str2,....)

ALIAS

칼럼이나 테이블에 이름 별칭 생성

#ALIAS 문법 - Column
SELECT column as alias
FROM tabelname
#ALIAS 문법 - Table
SELECT *
FROM tablename as alias;

DISTINCT

검색한 결과의 중복 제거

SELECT DISTINCT column1, column2
FROM tablename;

LIMIT

검색 결과를 정렬된 순으로 주어진 숫자만큼 조회

select column
from table
where condition
limit number;

profile
끄적끄적..

0개의 댓글