sql bag 3 join tabel

Post on 16-Nov-2014

3.279 Views

Category:

Documents

17 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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.

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.

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

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

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]

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).

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

Menampilkan record yang cocok dengan tabel sebelah kanan.

Syntax:table_reference RIGHT [OUTER] JOIN table_reference ON join_condition

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;

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;

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;

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;

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;

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;

top related