sql bag 3 join tabel

15
SQL (Structure SQL (Structure Query Language) – Query Language) – MySQL MySQL (Bagian 3: Table Joins) (Bagian 3: Table Joins) Oleh: Euis Marlina Oleh: Euis Marlina 08179424319 08179424319 http://euismarlina.edublogs.org http://euismarlina.edublogs.org

Upload: euis-marlina

Post on 16-Nov-2014

3.278 views

Category:

Documents


17 download

TRANSCRIPT

Page 1: SQL Bag 3 Join Tabel

SQL (Structure SQL (Structure Query Language) – Query Language) –

MySQL MySQL (Bagian 3: Table Joins)(Bagian 3: Table Joins)

Oleh: Euis MarlinaOleh: Euis Marlina0817942431908179424319

http://euismarlina.edublogs.orghttp://euismarlina.edublogs.org

Page 2: SQL Bag 3 Join Tabel

Perintah SQL join tabel digunakan untuk menampilkan data yang diambil dari beberapa tabel yang saling berhubungan.

Kita dapat menampilkan beberapa kolom dari tabel 1, beberapa kolom dari tabel 2, dan beberapa kolom dari tabel n.

Hampir semua perintah pada materi DML di bagian 2 dapat digabungkan penggunaannya dalam join table ini.

Page 3: SQL Bag 3 Join Tabel

JOIN digunakan untuk menampilkan semua record dari sebuah tabel yang cocok maupun tidak cocok dengan record dari tabel lainnya.

JOIN digunakan ketika beberapa tabel diakses melalui sebuah statemen SQL yaitu SELECT.

JOIN membandingkan semua kolom pada kedua tabel yang memiliki kesamaan nama kolom. Hasilnya adalah hanya menampilkan satu kolom untuk setiap pasang kolom yang namanya sama.

Page 4: SQL Bag 3 Join Tabel

table_reference JOIN table_factor WHERE | ON [join_condition]

table_reference [INNER | CROSS] JOIN table_factor WHERE [join_condition]

table_reference STRAIGHT_JOIN table_factor

Page 5: SQL Bag 3 Join Tabel

table_reference STRAIGHT_JOIN table_factor ON conditional_expr

table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference ON join_condition

table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor WHERE join_condition

Page 6: SQL Bag 3 Join Tabel

This type of join is the simplest join. The cross join result in cartesian product of all the records from two tables.

Jika pada tabel pertama ada 4 record, dan pada tabel kedua juga ada 4 record, maka jumlah record hasil query akan berjumlah 16 (hasil dari 4 x 4 =16)

Syntax:table_reference CROSS JOIN table_factor | WHERE [join_condition]

Page 7: SQL Bag 3 Join Tabel

This is the type of join where tables are combined based on a common column.

INNER JOIN memerlukan kriteria kolom tertentu sebagai penghubung kedua tabel.

Syntax:table_reference INNER JOIN table_factor WHERE [join_condition]

In MySQL, CROSS JOIN is a syntactic equivalent to INNER JOIN (they can replace each other).

Page 8: SQL Bag 3 Join Tabel

Menampilkan record dari tabel sebelah kiri yang cocok maupun yang tidak cocok dengan tabel sebelah kanan.

Record yang tidak cocok, akan ditampilkan dengan nilai NULL.

Syntax:table_reference LEFT [OUTER] JOIN table_reference ON join_condition

Page 9: SQL Bag 3 Join Tabel

Menampilkan record yang cocok dengan tabel sebelah kanan.

Syntax:table_reference RIGHT [OUTER] JOIN table_reference ON join_condition

Page 10: SQL Bag 3 Join Tabel

Table JoinsSelain beberapa jenis join di atas, anda juga bisa menggunakan konsep join tabel yang sangat sederhana yaitu dengan cara:

Jika ingin menampilkan semua field SELECT * from table_1, table2;

Jika ingin menampilkan field tertentu SELECT table_1.field_1, table_2.field_2, table_2.field_3 from table_1, table2;

Page 11: SQL Bag 3 Join Tabel

ContohContoh pada database mysql:SELECT * from help_relation,

help_keyword; atau

SELECT * from help_relation JOIN help_keyword;

SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword,help_relation WHERE help_keyword.help_keyword_id=help_relation.help_keyword_id;

Page 12: SQL Bag 3 Join Tabel

INNER/CROSS JOINSELECT * FROM help_keyword INNER JOIN help_relation;

SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword INNER JOIN help_relationWHERE help_keyword.help_keyword_id=help_relation.help_keyword_id;

SELECT * FROM help_keyword CROSS JOIN help_relation;

SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword CROSS JOIN help_relationWHERE help_keyword.help_keyword_id=help_relation.help_keyword_id;

Page 13: SQL Bag 3 Join Tabel

STRAIGHT_JOIN

SELECT * FROM help_keyword STRAIGHT_JOIN help_relation;

SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword STRAIGHT_JOIN help_relation ON

help_keyword.help_keyword_id=help_relation.help_keyword_id;

Page 14: SQL Bag 3 Join Tabel

LEFT/RIGHT [OUTER] JOINSELECT help_relation.help_keyword_id,

help_keyword.name from help_keyword LEFT JOIN help_relation ON

help_keyword.help_keyword_id=help_relation.help_keyword_id;

SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword RIGHT OUTER JOIN help_relation ON

help_keyword.help_keyword_id=help_relation.help_keyword_id;

Page 15: SQL Bag 3 Join Tabel

NATURAL LEFT/RIGHT [OUTER] JOIN

SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword NATURAL RIGHT JOIN help_relation WHERE

help_keyword.help_keyword_id=help_relation.help_keyword_id;

SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword NATURAL LEFT OUTER JOIN help_relation WHERE

help_keyword.help_keyword_id=help_relation.help_keyword_id;