2014-12-07 58 views
0

我有,我想加入4表:外連接多臺

tbl_transaksi_header 
-------------------- 
kode_transaksi 
kode_user 
nama_penerima 
email_penerima 
alamat_penerima 
bank 
telpon 


tbl_konfirmasi 
-------------------- 
id 
kode 
member 
namarekening 

tbl_user 
------------------- 
kode_user 
username_user 

tbl_transaksi_detail 
--------------------- 
kode_transaksi 
harga 
jumlah 
status 

我想告訴所有的數據,包括nama_rek NULL。

我嘗試這樣做:

SELECT t.status, t.kode_transaksi kode, u.username_user nama_user, t.nama_penerima penerima, t.telpon, t.bank, sum(d.harga*d.jumlah) total,k.namarekening nama_rek 
FROM tbl_transaksi_header t 
    JOIN tbl_user u 
     ON t.kode_user=u.kode_user 
    JOIN tbl_transaksi_detail d 
     ON t.kode_transaksi=d.kode_transaksi 
    LEFT OUTER JOIN tbl_konfirmasi k 
     ON t.kode_transaksi=k.kode 

做工精細,但沒有顯示空的結果,只顯示1分的結果。我想是這樣的:

status kode nama_user penerima telpn bank total nama_rek 
xxx  xxx  xxx   xxx   xxx  xxx  xxx  NULL 
xxx  xxx  xxx   xxx   xxx  xxx  xxx  NULL 
xxx  xxx  xxx   xxx   xxx  xxx  xxx  yyy 
xxx  xxx  xxx   xxx   xxx  xxx  xxx  NULL 

的樣本數據:

kode_transaksi kode_user nama_penerima email_penerima alamat_penerima bank telpon 
kd1    1   john   [email protected] florida   Bank A 088833221 
kd2    2   elsa   [email protected] uk    Bank B 088833222 
kd3    1   roy    [email protected] manhattan  Bank C 088833223 

id kode member namarekening 
1 kd1  paul paul 

kode_user username_user 
1   paul 
2   elena 

kode_transaksi harga jumlah status 
kd1    10  2  process 
kd1    5  2  process 
kd1    5  1  process 
kd2    4  3  pending 
kd2    3  4  pending 
kd3    2  3  pending 

我想這樣的輸出:

status kode nama_user penerima telpon  bank total nama_rek 
process kd1  paul  john  088833221 Bank A 35  paul 
pending kd2  elena  elsa  088833222 Bank B 24  NULL 
pending kd2  elena  roy   088833223 Bank C 6  NULL 
+0

你使用的是mysql,mssql,...? – pbaldauf 2014-12-07 14:44:04

+1

如果你會顯示一些示例源數據,這將有所幫助。 – Joe 2014-12-07 14:45:43

+0

@pbaldauf是mysql – john 2014-12-07 14:47:43

回答

0

你的加入是好的;問題在於你有SUM()在你的選擇沒有GROUP BY。試試這個:

SELECT t.kode_transaksi kode, u.username_user nama_user, t.nama_penerima penerima, t.telpon, t.bank, sum(d.harga*d.jumlah) total,k.namarekening nama_rek 
FROM tbl_transaksi_header t 
    JOIN tbl_user u 
     ON t.kode_user=u.kode_user 
    JOIN tbl_transaksi_detail d 
     ON t.kode_transaksi=d.kode_transaksi 
    LEFT OUTER JOIN tbl_konfirmasi k 
     ON t.kode_transaksi=k.kode 
GROUP BY 
    t.kode_transaksi, u.username_user, t.nama_penerima , t.telpon, t.bank,k.namarekening 
+0

只適用於GROUP BY t.kode_transaksi。在此先感謝 – john 2014-12-07 22:49:08

+0

是啊....一般來說好的做法是將所有未彙總的字段分組。有些RBDMS將會失敗(對我來說,這比允許它要好)。 – Joe 2014-12-07 22:55:45