我有4个表,如下所示,
MariaDB [nava_hotel]> desc transaksi;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| kode_transaksi | varchar(10) | NO | PRI | NULL | |
| kode_pemesanan | varchar(10) | YES | | NULL | |
| lama_menginap | int(3) | YES | | NULL | |
| total | bigint(20) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
MariaDB [nava_hotel]> desc pemesanan;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| kode_pemesanan | varchar(10) | NO | PRI | NULL | |
| kode_tamu | varchar(10) | YES | | NULL | |
| kode_kamar | varchar(10) | YES | | NULL | |
| tgl_checkin | datetime | YES | | NULL | |
| tgl_checkout | datetime | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
MariaDB [nava_hotel]> desc tamu;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| kode_tamu | varchar(10) | NO | PRI | NULL | |
| nama | varchar(30) | YES | | NULL | |
| alamat | text | YES | | NULL | |
| telepon | varchar(15) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
MariaDB [nava_hotel]> desc kamar;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| kode_kamar | varchar(10) | NO | PRI | NULL | |
| jenis_kamar | varchar(20) | YES | | NULL | |
| fasilitas | text | YES | | NULL | |
| harga | bigint(20) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
如何通过连接四个表来编写新查询并获得与下面相同的结果:
kode_transaksi,kode_pemesanan,kode_kamar,jenis_kamar,kode_tamu,nama
我对数据库有点新意,需要帮助。提前致谢
答案 0 :(得分:2)
可以使用以下JOIN查询:
select t.kode_transaksi,p.kode_pemesanan,p.kode_kamar,k.jenis_kamar,p.kode_tamu,m.nama
from pemesanan p
inner join transaksi t on ( t.kode_pemesanan = p.kode_pemesanan )
inner join tamu m on ( m.kode_tamu = p.kode_tamu )
inner join kamar k on ( k.kode_kamar = p.kode_kamar );