2016-11-16 84 views
0

我真的很迷惑從我的查詢中獲得完整記錄。獲取查詢中的所有數據select select mysql select

我已經盡力了,但我仍然無法得到我想要的東西。

我使用的是MySQL 5.5,我真的不知道該怎麼做。

我會分享我的database.sql,因爲我的查詢加入5個以上的表。

database_schema.sql

,這是我的查詢在哪裏受審。

select id_akun, kode_akun, nama_akun, SUM(saldo_awal) as 'SaldoAwal', SUM(debet) as 'Debet', SUM(kredit) as 'Kredit', (SUM(saldo_awal) + SUM(debet) - SUM(kredit)) as 'SaldoAkhir' from (
select a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) as saldo_awal, b.amount as 'debet', 0 as 'kredit' 
from account a, transaksi_kas_detail b, transaksi_kas c 
where a.id=b.account_id AND b.transaksi_kas_id=c.id 
    AND dt='D' AND c.trx_type IN ('1', '2') 
UNION 
select a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit' 
from account a, transaksi_kas_detail b, transaksi_kas c 
where a.id=b.account_id AND b.transaksi_kas_id=c.id 
    AND dt='K' AND c.trx_type IN ('1', '2') 
UNION 
select a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, b.amount as 'debet', 0 as 'kredit' 
from account a, transaksi_kas_detail b, transaksi_kas c, kas d 
where a.id=b.account_id AND b.transaksi_kas_id=c.id AND c.kas_id=d.id 
    AND dt='D' AND c.trx_type IN ('3', '4') AND d.jenis != '3' 
UNION 
select a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit' 
from account a, transaksi_kas_detail b, transaksi_kas c, kas d 
where a.id=b.account_id AND b.transaksi_kas_id=c.id AND c.kas_id=d.id 
    AND dt='K' AND c.trx_type IN ('3', '4') AND d.jenis != '3' 
UNION 
select a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, b.amount as 'debet', 0 as 'kredit' 
from account a, transaksi_kas_detail b, transaksi_kas c 
where a.id=b.account_id AND b.transaksi_kas_id=c.id 
    AND dt='D' AND c.trx_type IN ('5', '6') 
UNION 
select a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit' 
from account a, transaksi_kas_detail b, transaksi_kas c 
where a.id=b.account_id AND b.transaksi_kas_id=c.id 
    AND dt='K' AND c.trx_type IN ('5', '6') 
) fff WHERE group_account_id = '15' 
GROUP BY id_akun, kode_akun, nama_akun 
ORDER BY kode_akun asc 

後,我運行此查詢..

我會得到這樣的結果。

enter image description here

我只是得到2紀錄,我需要通過ORDER BY kode_akun獲取所有數據,其中,我從表account

得到,如果表帳戶中有10行,那麼我的查詢必須顯示10行。

也許有人可以幫我

例如這樣的..

enter image description here

+0

你能做出[SQLFiddle(http://sqlfiddle.com/)? –

+1

UNION將重複複製。這是問題嗎?如果是這樣切換到聯盟所有 – Kickstart

+0

@PraveenKumar我試過,但我的表到很多,我試圖導出從我的數據庫,然後將查詢複製到sqlFiddle,但我不知道爲什麼架構不創建 – akiong

回答

1

從你的詳細說明,我想你需要的是LEFT JOIN的transaksi_kas_detail和transaksi_kas表(和KAS表使用時)。

請注意,您的示例數據具有進一步細分的細節,並且不確定您是如何獲取額外數據的。

但是像這樣

SELECTid_akun, kode_akun, nama_akun, SUM(saldo_awal) as 'SaldoAwal', SUM(debet) as 'Debet', SUM(kredit) as 'Kredit', (SUM(saldo_awal) + SUM(debet) - SUM(kredit)) as 'SaldoAkhir' 
from 
(
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) as saldo_awal, b.amount as 'debet', 0 as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('1', '2') 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('1', '2') 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, b.amount as 'debet', 0 as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('3', '4') 
    LEFT OUTER JOIN kas d ON c.kas_id = d.id AND d.jenis != '3' 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('3', '4') 
    LEFT OUTER JOIN kas d ON c.kas_id = d.id AND d.jenis != '3' 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, b.amount as 'debet', 0 as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('5', '6') 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('5', '6') 
) fff 
WHERE group_account_id = '15' 
GROUP BY id_akun, kode_akun, nama_akun 
ORDER BY kode_akun asc 

編輯 - 檢查的日期在ON條款: -

SELECT id_akun, kode_akun, nama_akun, SUM(saldo_awal) as 'SaldoAwal', SUM(debet) as 'Debet', SUM(kredit) as 'Kredit', (SUM(saldo_awal) + SUM(debet) - SUM(kredit)) as 'SaldoAkhir' 
from 
(
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) as saldo_awal, IF(c.id IS NULL, 0, b.amount) as 'debet', 0 as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('1', '2') AND c.trx_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00' 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', IF(c.id IS NULL, 0, b.amount) as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('1', '2') AND c.trx_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00' 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, IF(d.id IS NULL, 0, b.amount) as 'debet', 0 as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('3', '4') AND c.trx_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00' 
    LEFT OUTER JOIN kas d ON c.kas_id = d.id AND d.jenis != '3' 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', IF(d.id IS NULL, 0, b.amount) as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('3', '4') AND c.trx_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00' 
    LEFT OUTER JOIN kas d ON c.kas_id = d.id AND d.jenis != '3' 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, IF(c.id IS NULL, 0, b.amount) as 'debet', 0 as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D' AND b.cek_due_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('5', '6') 
    UNION ALL 
    SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', IF(c.id IS NULL, 0, b.amount) as 'kredit' 
    FROM account a 
    LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K' AND b.cek_due_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00' 
    LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('5', '6') 
) fff 
WHERE group_account_id = '15' 
GROUP BY id_akun, kode_akun, nama_akun 
ORDER BY kode_akun asc 
+0

你的代碼已經運行良好,但我會變得不同當我添加'WHERE group_account_id = 15和trx_date> ='2016-01-01 00:00:00'和trx_date <='2016-11-16 00:00:00'' – akiong

+0

@akiong - 結果trx_date值來自LEFT OUTER JOINed表,因此當沒有匹配的記錄時它們將是NULL。如果您然後在外部查詢的WHERE子句中檢查這些值,它將排除這些記錄。將檢查放在_transaksi_kas_detail_的每個LEFT OUTER JOIN的ON子句中。這是它將從SUM中排除它們,但保留帳戶詳細信息。 – Kickstart

+0

哦,我看到,,,因爲記錄與trx_date不匹配,那麼我有可能獲得記錄甚至trx_date'NULL'就像你的查詢,即使記錄不在'transaksi_kas_detail'中,記錄也會顯示 – akiong