I want to make a report from 3 tables like this
SELECT transaksi.id_transaksi,transaksi.waktu_pengiriman, pengirim.nama_pengirim,penerima.nama_penerima, penerima.alamat_penerima,transaksi.harga,transaksi.status_pengiriman FROM transaksi,pengirim,penerima WHERE transaksi.waktu_pengiriman BETWEEN '2020-11-01' AND '2020-11-30'
I saw this answer on this forum that telling me I could SELECT 3 tables directly like that but the output unexpectedly duplicated so much. This is how it looks like
Advertisement
Answer
You are cross joining everything — you want left joins… I’m guessing what the joins are but it would look something like this:
SELECT transaksi.id_transaksi, transaksi.waktu_pengiriman, pengirim.nama_pengirim, penerima.nama_penerima, penerima.alamat_penerima, transaksi.harga, transaksi.status_pengiriman FROM transaksi LEFT JOIN pengirim ON transaksi.id_pengirim = pengirim.id LEFT JOIN penerima ON transaksi.id_penerima = penerima.id WHERE transaksi.waktu_pengiriman BETWEEN '2020-11-01' AND '2020-11-30'