1. Trang chủ
  2. » Mẫu Slide

slide môn học MySQL bài 5 using joins

22 222 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 22
Dung lượng 464 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

MySQL Database / Session 10 / Slide 4 of 22Concept of Joining Tables  Combining two or more records of different tables from the same database, into one complete structure, is known as

Trang 1

Using Joins

Session 10

Trang 2

MySQL Database / Session 10 / Slide 2 of 22

 Primary key is a unique identifier in a table Foreign key is a

primary key in a different table

 The DISTINCT option in the SELECT command enables user to

view the distinct values for a column

 Grouping facilitates to group rows with similar values for a

specific column into a single row in order to operate on them

together

 Keys are pieces of information that helps to identify a record in

a table

Review

Trang 3

MySQL Database / Session 10 / Slide 3 of 22

Trang 4

MySQL Database / Session 10 / Slide 4 of 22

Concept of Joining Tables

 Combining two or more records of different tables from the

same database, into one complete structure, is known as joining

of tables

 The tables can be joined either by using WHERE clause with

SELECT command or by using JOIN keyword with SELECT

command

Trang 5

MySQL Database / Session 10 / Slide 5 of 22

Equi Join-I

found in both the tables The syntax for Equi-Join is:

SELECT column_name, [*] FROM table1 [as alias],table2 [as alias] WHERE

(join_condition);

Trang 6

MySQL Database / Session 10 / Slide 6 of 22

Equi Join-II

 Display first name, department name and designation of all

employees from EMP_DETAILS and EMP_DEPARTMENT tables

D_NAME,EMP_DEPARTMENT.DESIGNATION FROM

EMP_DETAILS,EMP_DEPARTMENT WHERE (EMP_DETAILS.E_ID

= EMP_DEPARTMENT.E_ID);

Trang 7

MySQL Database / Session 10 / Slide 7 of 22

Inner Join-I

fields of both tables that satisfy the query for both the tables

The syntax for inner-join is:

SELECT column_name1, column_name2,

column_name3 FROM table_name1 INNER JOIN

table_name2 ON

table_name1.primary_keyfield =

table_name2.foreign_keyfield;

Trang 8

MySQL Database / Session 10 / Slide 8 of 22

Inner Join-II

 Display first name, department name, and designation of all

employees from EMP_DETAILS and EMP_DEPARTMENT tables

D_NAME,EMP_DEPARTMENT.DESIGNATION FROM

EMP_DETAILS INNER JOIN EMP_DEPARTMENT ON

EMP_DETAILS.E_ID = EMP_DEPARTMENT.E_ID;

Trang 9

MySQL Database / Session 10 / Slide 9 of 22

Trang 10

MySQL Database / Session 10 / Slide 10 of 22

Inner Join using table aliases

using table aliases

Trang 11

MySQL Database / Session 10 / Slide 11 of 22

Outer Join

 An OUTER JOIN displays even those rows of the tables that may not have any matching value in the other tables to appear in the

result set There are three types of outer join They are:

 Left Outer Join

 Right Outer Join

 Full Outer Join

Trang 12

MySQL Database / Session 10 / Slide 12 of 22

Left Outer Join-I

OUTER JOIN operator in the result set, with or without any

match with the table specified on the right The syntax is:

SELECT COLUMN1, COLUMN2 FROM TABLE1

LEFT OUTER JOIN TABLE2 ON (JOIN_

CONDITION);

Trang 13

MySQL Database / Session 10 / Slide 13 of 22

Left Outer Join-II

 Display all the records from EMP_DETAILS, EMP_SALARY, and

EMP_DEPARTMENT tables by joining them with the common attribute

E_ID

SELECT E_FNAME,E_LNAME,E_ADDRESS FROM EMP_DETAILS LEFT

JOIN EMP_SALARY ON EMP_DETAILS.E_ID = EMP_SALARY.E_ID

LEFT JOIN EMP_DEPARTMENT ON EMP_SALARY.E_ID =

EMP_DEPARTMENT.E_ID;

Trang 14

MySQL Database / Session 10 / Slide 14 of 22

Right Outer Join -I

OUTER JOIN operator in the result set, with or without any

match with the table specified on the left The syntax is:

SELECT COLUMN1, COLUMN2 FROM TABLE1

RIGHT OUTER JOIN TABLE2 ON (JOIN_

CONDITION);

Trang 15

MySQL Database / Session 10 / Slide 15 of 22

Right Outer Join-II

E_ADDRESS and E_PHONE_NO from

EMP_DETAILS table where the

E_LNAME,E_ADDRESS,E_PHONE_

NO FROM EMP_DETAILS E RIGHT

OUTER JOIN EMP_DEPARTMENT D

ON(E.E_ID > 102);

Trang 16

MySQL Database / Session 10 / Slide 16 of 22

Self-Join-I

table to itself The syntax is:

SELECT DISTINCT COLUMN1,COLUMN2 FROM TABLEWHERE (JOIN_CONDITION);

Trang 17

MySQL Database / Session 10 / Slide 17 of 22

Self-Join-II

are located in the same city

SELECT DISTINCT E1.E_ID,

E1.E_FNAME,E1.E_LNAME,

E.E_ADDRESS FROM EMP_DETAILS

AS E1,EMP_DETAILS AS E2

WHERE E1.E_ADDRESS =

E2.E_ADDRESS AND E1.E_ADDRESS

IS NOT NULL AND E2.E_ADDRESS

IS NOT NULL AND E1.E_ID <>

E2.E_ID ORDER BY

E1.E_ADDRESS,E1.E_ID;

Trang 18

MySQL Database / Session 10 / Slide 18 of 22

Sub-Select-I

When a SELECT query is placed inside another SELECT

query, then the inner SELECT query is said to be Sub-Select

query The syntax is:

SELECT [*] column_name… FROM table_name1

WHERE column_name IN (SELECT column_name

FROM table_name2);

Trang 19

MySQL Database / Session 10 / Slide 19 of 22

Sub-Select-II

 Display all records from EMP_DETAILS and EMP_DEPARTMENT tables using sub-selects

E_ID FROM EMP_DEPARTMENT);

Trang 20

MySQL Database / Session 10 / Slide 20 of 22

UNION-I

SELECT command into a single result set The syntax is:

SELECT * FROM TABLE1 UNION [ALL | DISTINCT]

SELECT * FROM TABLE2 UNION [ALL | DISTINCT]

Trang 21

MySQL Database / Session 10 / Slide 21 of 22

UNION-II

 Display all the records from EMP_DETAILS table where the employee

addresses are California and Troy

(SELECT * FROM EMP_DETAILS WHERE E_ADDRESS =

‘TROY’ORDER BY E_ID LIMIT 2)UNION (SELECT * FROM

EMP_DETAILS WHERE E_ADDRESS =‘CALIFORNIA’ ORDER

BY E_ID LIMIT 1) ORDER BY E_ID;

Trang 22

MySQL Database / Session 10 / Slide 22 of 22

Summary

the same database into one comprehensive structure

command or by using the JOIN keyword

same values

tables that satisfy the query for both the tables

a single result set

Ngày đăng: 30/11/2016, 22:11

TỪ KHÓA LIÊN QUAN