Persiapan Data
Tambahkan tabel-tabel berikut ini pada database DBPENJUALAN :
mysql> create table pelanggan
-> (kdplg char(5) not null primary key,
-> namaplg varchar(20),
-> alamat varchar(20),
-> kota varchar(10),
-> telpon char(12));
Query OK, 0 rows affected (0.14 sec)
mysql> desc pelanggan;
+----------------+----------------+-------+-------+-----------------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+----------------+-------+-------+-----------------+-------+
| kdplg | char(5) | NO | PRI | NULL | |
| namaplg | varchar(20) | YES | | NULL | |
| alamat | varchar(20) | YES | | NULL | |
| kota | varchar(10) | YES | | NULL | |
| telpon | char(12) | YES | | NULL | |
+----------------+----------------+-------+--------+----------------+-------+
5 rows in set (0.05 sec)
mysql> create table fakturjual
-> (nofak char(6) not null,
-> tanggal date,
-> kdplg char(5) not null,
-> primary key (nofak, kdplg));
Query OK, 0 rows affected (0.55 sec)
mysql> desc fakturjual;
+---------------+-----------------+-------+-------+-----------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-----------------+-------+-------+-----------------+-------+
| nofak | char(6) | NO | PRI | NULL | |
| tanggal | date | YES | | NULL | |
| kdplg | char(5) | NO | PRI | NULL | |
+---------------+-----------------+-------+--------+----------------+-------+
3 rows in set (0.11 sec)
mysql> create table jual
-> (nofak char(6) not null,
-> kodebrg char(5) not null,
-> jlhjual int(4),
-> primary key (nofak, kodebrg));
Query OK, 0 rows affected (0.48 sec)
mysql> desc jual;
+----------------+----------------+-------+--------+----------------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+----------------+-------+--------+----------------+-------+
| nofak | char(6) | NO | PRI | NULL | |
| kodebrg | char(5) | NO | PRI | NULL | |
| jlhjual | int(4) | YES | | NULL | |
+----------------+----------------+--------+-------+----------------+-------+
3 rows in set (0.09 sec)
Isilah data untuk tabel diatas sesuai dengan data berikut :
mysql> select * from pelanggan;
+----------------+----------------+-------------------------+-----------------+------------------+
| kdplg | namaplg | alamat | kota | telpon |
+----------------+----------------+-------------------------+----------------+-------------------+
| PL001 | Bambang | Jl. Cemara No. 20 | Medan | 081360869289 |
| PL002 | Boby Siregar | Jl. Pancing No.10 | Medan | 081361869090 |
| PL003 | Alfarisi | Jl. Merdeka No.20 | Binjai | 081370869393 |
| PL004 | Indriani | Jl. Sutomo no. 5 | L. Pakam | 081350556677 |
| PL005 | Pipit Novel | Jl. SM. Raja | Siantar | 081312345678 |
| PL006 | Alfonso | Jl. Pipit No.35 | Medan | 081387654321 |
+----------------+----------------+-------------------------+-----------------+------------------+
6 rows in set (0.00 sec)
mysql> select * from fakturjual;
+----------------+----------------+----------------+
| nofak | tanggal | kdplg |
+----------------+----------------+----------------+
| FKT001 | 2011-05-10 | PL001 |
| FKT002 | 2011-05-22 | PL003 |
| FKT003 | 2011-05-25 | PL005 |
| FKT004 | 2011-05-27 | PL002 |
+----------------+----------------+----------------+
4 rows in set (0.00 sec)
mysql> select * from jual;
+----------------+----------------+----------------+
| nofak | kodebrg | jlhjual |
+----------------+----------------+----------------+
| FKT001 | BR001 | 5 |
| FKT001 | BR002 | 10 |
| FKT001 | BR004 | 8 |
| FKT001 | BR005 | 20 |
| FKT002 | BR004 | 7 |
| FKT002 | BR007 | 15 |
| FKT003 | BR001 | 3 |
| FKT003 | BR002 | 6 |
| FKT003 | BR010 | 2 |
+----------------+----------------+----------------+
9 rows in set (0.00 sec)
Aturan dalam melakukan query antar tabel :
1. Setiap field disebutkan bersama dengan nama tabelnya, dipisahkan dengan tanda titik (.).
Format : namatabel.namafield
Contoh : pelanggan.kdplg
2. Setiap tabel yang terlibat dalam proses query harus disebutkan dalam klausa FROM, dengan pemisah koma (,).
Contoh : FROM fakturjual,pelanggan
3. Kondisi dalam klausa WHERE mempengaruhi hasil query yang tercipta.
Ketik perintah penggabungan dua tabel berikut ini :
mysql> select fakturjual.nofak, fakturjual.tanggal, fakturjual.kdplg,
-> pelanggan.namaplg, pelanggan.kota from fakturjual,pelanggan
-> where fakturjual.kdplg=pelanggan.kdplg;
+----------------+----------------+----------------+----------------+-----------------+
| nofak | tanggal | kdplg | namaplg | kota |
+----------------+----------------+----------------+----------------+-----------------+
| FKT001 | 2011-05-10 | PL001 | Bambang | Medan |
| FKT002 | 2011-05-22 | PL003 | Alfarisi | Binjai |
| FKT003 | 2011-05-25 | PL005 | Pipit Novel | Siantar |
| FKT004 | 2011-05-27 | PL002 | Boby Siregar | Medan |
+----------------+----------------+----------------+-----------------+----------------+
4 rows in set (0.00 sec)
Terimakasih. Semoga bermanfaat.
0 komentar:
Post a Comment