我有加入兩個選擇聯合結果在MySQL中的問題,任何人都可以幫助我嗎? 這是我的查詢左加入兩個工會選擇
SELECT * FROM(
SELECT T1.*,T2.target FROM (
(SELECT skpd.kd_skpd AS kd_skpd,CONCAT_WS(' ','','','','','') as
no_rekening, skpd.nm_skpd as nama from skpd WHERE skpd.kd_skpd='SD001'
UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,'','','','') as no_rekening,
B.nm_akun from m_akun_rek B
UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,B.k_kel,'','','') as no_rekening,
B.nm_kel from m_kel_rek B, rekening_skpd A WHERE B.k_akun = A.k_akun AND
B.k_kel=A.k_kel and A.kd_skpd='SD001'
UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,B.k_kel,B.k_jenis,'','') as
no_rekening, B.nm_jenis from m_jenis_rek B, rekening_skpd A WHERE B.k_akun =
A.k_akun AND B.k_kel=A.k_kel AND B.k_jenis = A.k_jenis and A.kd_skpd='SD001'
UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,B.k_kel,B.k_jenis,B.k_obyek,'') as
no_rekening, B.nm_obyek from m_obyek_rek B, rekening_skpd A WHERE B.k_akun =
A.k_akun AND B.k_kel=A.k_kel AND B.k_jenis = A.k_jenis AND
B.k_obyek=A.k_obyek and A.kd_skpd='SD001'
UNION
SELECT '' as kd,CONCAT_WS('
',B.k_akun,B.k_kel,B.k_jenis,B.k_obyek,B.k_rekening) as no_rekening,
B.nm_rekening from m_rekening B, rekening_skpd A WHERE B.k_akun = A.k_akun
AND B.k_kel=A.k_kel AND B.k_jenis = A.k_jenis AND B.k_obyek=A.k_obyek AND
B.k_rekening=A.k_rekening and A.kd_skpd='SD001'
) T1
LEFT JOIN
(SELECT skpd.kd_skpd AS kd_skpd,CONCAT_WS(' ','','','','','') as
no_rekening, skpd.nm_skpd as nama,'' as target from skpd WHERE
skpd.kd_skpd='SD001'
UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,'','','','') as no_rekening,
B.nm_akun, SUM(A.nominal_target) as target from m_akun_rek B, target_tahunan
A WHERE A.kd_skpd ='SD001' AND A.k_akun = B.k_akun AND A.tahun_target =
'2017'
UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,B.k_kel,'','','') as no_rekening,
B.nm_kel, SUM(C.nominal_target) as target from m_kel_rek B, rekening_skpd A,
target_tahunan C WHERE B.k_akun = A.k_akun AND B.k_kel=A.k_kel and
A.kd_skpd='SD001' AND C.k_akun = B.k_akun AND C.k_kel=A.k_kel and
C.kd_skpd='SD001' AND C.tahun_target = '2017'
UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,B.k_kel,B.k_jenis,'','') as
no_rekening, B.nm_jenis, SUM(C.nominal_target) as target from m_jenis_rek B,
rekening_skpd A, target_tahunan C WHERE B.k_akun = A.k_akun AND
B.k_kel=A.k_kel AND B.k_jenis = A.k_jenis and A.kd_skpd='SD001' AND C.k_akun
= B.k_akun AND C.k_kel=B.k_kel AND C.k_jenis = B.k_jenis and
C.kd_skpd='SD001' AND C.tahun_target = '2017'
UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,B.k_kel,B.k_jenis,B.k_obyek,'') as
no_rekening, B.nm_obyek, SUM(C.nominal_target) as target from m_obyek_rek B,
rekening_skpd A, target_tahunan C WHERE B.k_akun = A.k_akun AND
B.k_kel=A.k_kel AND B.k_jenis = A.k_jenis AND B.k_obyek=A.k_obyek and
A.kd_skpd='SD001' AND C.k_akun = B.k_akun AND C.k_kel=B.k_kel AND C.k_jenis
= B.k_jenis AND C.k_obyek = B.k_obyek and C.kd_skpd='SD001' AND
C.tahun_target = '2017'
UNION
SELECT '' as kd,CONCAT_WS('
',B.k_akun,B.k_kel,B.k_jenis,B.k_obyek,B.k_rekening) as
no_rekening,B.nm_rekening, SUM(C.nominal_target) as target from m_rekening
B,rekening_skpd A, target_tahunan C WHERE B.k_akun = A.k_akun AND
B.k_kel=A.k_kel AND B.k_jenis = A.k_jenis AND B.k_obyek=A.k_obyek AND
B.k_rekening=A.k_rekening and A.kd_skpd='SD001' AND C.k_akun = B.k_akun AND
C.k_kel=B.k_kel AND C.k_jenis = B.k_jenis AND C.k_obyek = B.k_obyek AND
C.k_rekening = B.k_rekening and C.kd_skpd='SD001' AND C.tahun_target =
'2017'
) T2
ON T1.kd_skpd = T2.kd_skpd
AND T1.no_rekening = T2.no_rekening
AND T1.nama = T2.nama
)
) AS T
,這是錯誤消息
[Err] 1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'UNION
SELECT '' as kd,CONCAT_WS(' ',B.k_akun,B.k_kel,'','','') as no_rekening, '
at line 6
我已經跑了使用在PostgreSQL和它的工作類似的查詢,但它不能在MySQL的
偉大的工作!,你的回答解決我的問題,三江源所以很多布魯。 –
我想要顯示這樣的結果 –