4. 기본 명령문(3)     -바닥부터 배우는 Mysql -

 

JOIN

UNION


연습용 데이터베이스 테이블

Students:

StudentID

Name

City

Tel

1

Kim ChulSu

Seoul

002-3256

2

Lee YoungHee

Pusan

012-7854

3

Park JinSu

Pusan

022-4725

4

Choi YoungChul

Incheon

021-3478

5

Lee SuYoung

Seoul

003-9512

 

Grade:

StudentID

Name

Kor

Eng

Math

1

Kim ChulSu

85

75

88

3

Park JinSu

76

95

89

5

Lee SuYoung

93

95

98

6

Lee JungSu

77

68

94

7

Kim JinTae

85

92

90

 

JOIN :

두 개 이상의 테이블에서 필요한 열(컬럼)을 조합해서 사용.

 

INNER JOIN: 여러 개의 테이블에서 ON절의 조건에 맞는 열을 출력한다.

선택하려는 테이블의 컬럼 명을 (.)으로 연결한다.

SELECT column_name(s)

FROM table1

INNER JOIN table2

ON table1.column_name=table2.column_name;

또는

SELECT column_name(s)

FROM table1

JOIN table2

ON table1.column_name=table2.column_name;

 

4-1

SELECT Students.StudentID, Students.Name, Grade.Kor

FROM Students

INNER JOIN Grade

ON Students.StudentID=Grade.StudentID;

결과

StudentID

Name

Kor

1

Kim ChulSu

85

3

Park JinSu

76

5

Lee SuYoung

93

 

using()을 사용하면 적용되는 컬럼은 한 번만 출력된다.

4-2

SELECT * FROM Students INNER JOIN Grade using(StudentID);

StudentID

Name

City

Tel

Name

Kor

Eng

Math

1

Kim ChulSu

Seoul

002-3256

Kim ChulSu

85

75

88

3

Park JinSu

Pusan

022-4725

Park JinSu

76

95

89

5

Lee SuYoung

Seoul

003-9512

Lee SuYoung

93

95

98

 

WHERE절을 사용한 경우.

SELECT * FROM Students, Grade WHERE Students.StudentID=Grade.StudentID;

(위와 같은 결과가 나온다.)

 

LEFT JOIN: 왼쪽 테이블은 모두 츨력하고 오른쪽 테이블은 조건에 맞는 것만 출력.(빈 칸은 NULL로 채워진다.)

SELECT column_name(s)

FROM table1

LEFT JOIN table2

ON table1.column_name=table2.column_name;

또는

SELECT column_name(s)

FROM table1

LEFT OUTER JOIN table2

ON table1.column_name=table2.column_name;

4-3

SELECT * FROM Students LEFT JOIN Grade using(StudentID);

StudentID

Name

City

Tel

Name

Kor

Eng

Math

1

Kim ChulSu

Seoul

002-3256

Kim ChulSu

85

75

88

2

Lee YoungHee

Pusan

012-7854

 

 

 

 

3

Park JinSu

Pusan

022-4725

Park JinSu

76

95

89

4

Choi YoungChul

Incheon

021-3478

 

 

 

 

5

Lee SuYoung

Seoul

003-9512

Lee SuYoung

93

95

98

 

RIGHT JOIN: 오른쪽 테이블은 모두 출력하고 왼쪽 테이블은 조건에 맞는 것만 출력.

SELECT column_name(s)

FROM table1

RIGHT JOIN table2

ON table1.column_name=table2.column_name;

또는

SELECT column_name(s)

FROM table1

RIGHT OUTER JOIN table2

ON table1.column_name=table2.column_name;

 

4-4

SELECT * FROM Students RIGHT JOIN Grade using(StudentID);

StudentID

Name

Kor

Eng

Math

Name

City

Tel

1

Kim ChulSu

85

75

88

Kim ChulSu

Seoul

002-3256

3

Park JinSu

76

95

89

Park JinSu

Pusan

022-4725

5

Lee SuYoung

93

95

98

Lee SuYoung

Seoul

003-9512

6

Lee JungSu

77

68

94

 

 

 

7

Kim JinTae

85

92

90

 

 

 

 

UNION 연산자:

두개 이상의 select문을 결합한다.

select문은 유사한 데이터 타입을 가진 같은 수의 컬럼을 선택해야 한다.

중복된 데이터는 출력하지 않는다. (UNION=UNION DISTINCT)

SELECT column_name(s) FROM table1

UNION

SELECT column_name(s) FROM table2;

4-5

SELECT Name FROM Students

UNION

SELECT Name FROM Grade

ORDER BY Name;

결과

Name

Choi YoungChul

Kim ChulSu

Kim JinTae

Lee JungSu

Lee SuYoung

Lee YoungHee

Park JinSu

 

UNION ALL은 중복된 데이터를 출력한다.

SELECT column_name(s) FROM table1

UNION ALL

SELECT column_name(s) FROM table2;

4-6

SELECT Name FROM Students

UNION ALL

SELECT Name FROM Grade

ORDER BY Name;

결과

Name

Choi YoungChul

Kim ChulSu

Kim ChulSu

Kim JinTae

Lee JungSu

Lee SuYoung

Lee SuYoung

Lee YoungHee

Park JinSu

Park JinSu

 

Posted by star story :

Snippet :: Code View :: "+location.href+'
'+b+"

");top.consoleRef.document.close()}$(".snippet-container").each(function(b){$(this).find("a.snippet-text").click(function(){var d=$(this).parents(".snippet-wrap").find(".snippet-formatted");var c=$(this).parents(".snippet-wrap").find(".snippet-textonly");d.toggle();c.toggle();if(c.is(":visible")){$(this).html("html")}else{$(this).html("text")}return false});$(this).find("a.snippet-window").click(function(){var c=$(this).parents(".snippet-wrap").find(".snippet-textonly").html();a(c);$(this).blur();return false})});$(".snippet-toggle").each(function(b){$(this).click(function(){$(this).parents(".snippet-container").find(".snippet-wrap").toggle()})})});