5
Inner Join Comp 306

Inner Join - classes.eastus.cloudapp.azure.comclasses.eastus.cloudapp.azure.com/.../slides/inner_join_example.pdf · mysql> select * from courses; courseId descript instrID 11500

  • Upload
    haque

  • View
    232

  • Download
    0

Embed Size (px)

Citation preview

Inner Join Comp 306

mysql> select * from courses;

courseId descript instrID

11500 Discrete Structures 1111

17100 Comp Principles 1 1111

17200 Comp Principles 2 2222

20500 Adv Web Prog 3333

20700 3D Game Dev 2222

21000 Comp Org 1111

22000 Data Struct 2222

31100 Algm 3333

studentId studentName Dorm

1212 John Stanton

1222 George Landon

2323 Susan New House

3434 Gwendolyn Jobs

4545 Gabriel Brooks

studentID courseID s2cID

1212 11500 1

1212 17200 2

3434 17100 3

2323 17200 4

3434 11500 5

4545 21000 6

4545 31100 7

mysql> select * from students;

mysql> select * from student2course;

mysql> SELECT students.studentName, student2course.studentID, student2course.courseID -> FROM students -> INNER JOIN student2course -> ON students.studentId = student2course.studentID;

studentName studentID courseID

John 1212 11500

John 1212 17200

Gwendolyn 3434 17100

Susan 2323 17200

Gwendolyn 3434 11500

Gabriel 4545 21000

Gabriel 4545 31100

studentId studentName Dorm

1212 John Stanton

1222 George Landon

2323 Susan New House

3434 Gwendolyn Jobs

4545 Gabriel Brooks

studentID courseID s2cID

1212 11500 1

1212 17200 2

3434 17100 3

2323 17200 4

3434 11500 5

4545 21000 6

4545 31100 7

To get this table in ascending order Add to the end of the query: ORDER BY students.studentName;

mysql> SELECT students.studentName, courses.descript -> FROM ((students INNER JOIN student2course ON students.studentId = student2course.studentID) -> INNER JOIN courses ON student2course.courseID = courses.courseId);

studentName studentID courseID

John 1212 11500

John 1212 17200

Gwendolyn 3434 17100

Susan 2323 17200

Gwendolyn 3434 11500

Gabriel 4545 21000

Gabriel 4545 31100

studentId studentName Dorm

1212 John Stanton

1222 George Landon

2323 Susan New House

3434 Gwendolyn Jobs

4545 Gabriel Brooks

studentID courseID s2cID

1212 11500 1

1212 17200 2

3434 17100 3

2323 17200 4

3434 11500 5

4545 21000 6

4545 31100 7

The first INNER JOIN Combines the students table with the student2course table With the courses table.

mysql> SELECT students.studentName, courses.descript -> FROM ((students INNER JOIN student2course ON students.studentId = student2course.studentID) -> INNER JOIN courses ON student2course.courseID = courses.courseId);

studentName studentID courseID

John 1212 11500

John 1212 17200

Gwendolyn 3434 17100

Susan 2323 17200

Gwendolyn 3434 11500

Gabriel 4545 21000

Gabriel 4545 31100

studentId descript

John Discrete Structures

John Comp Principles 2

Gwendolyn Comp Principles 1

Susan Comp Principles 2

Gwendolyn Discrete Structures

Gabriel Comp Org

Gabriel Algm

The second INNER JOIN Combines the table from the first join with the courses table.

courseId descript instrID

11500 Discrete Structures 1111

17100 Comp Principles 1 1111

17200 Comp Principles 2 2222

20500 Adv Web Prog 3333

20700 3D Game Dev 2222

21000 Comp Org 1111

22000 Data Struct 2222

31100 Algm 3333