Skip to content
Advertisement

I want to make monthly report from SQL using 3 tables [closed]

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 enter image description here

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'
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement