左加入两个联合选择

时间:2017-05-31 06:38:12

标签: mysql database

我在mysql中加入两个select union结果有问题,有人可以帮帮我吗? 这是我的查询

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中不起作用

2 个答案:

答案 0 :(得分:0)

错误就在这里

    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 T1.kd,t1.no_rekening,T1.nm_rekening from
    (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) T2 
    on T1....... = T2....... WHERE 
                skpd.kd_skpd='SD001'
                UNION

答案 1 :(得分:0)

有两个不属于的括号:

SELECT T1.*,T2.target FROM ((SELECT

应该是

SELECT T1.*,T2.target FROM (SELECT

)) AS T

应该是

) AS T

然后查询包含无效的聚合,例如:

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'

在这里,您需要一个结果行,因为您使用SUM而没有任何GROUP BY。但CONCAT_WS(...)B.nm_akun不是聚合。那么你实际上试图访问哪一行的值?这是无效的SQL。早期的MySQL版本没有引发错误,但它们应该有。

(除此之外,这是一个可怕的查询。逗号分隔的连接表明查询的某些部分可能是在20世纪80年代编写的。然后那些很多的UNION为DBMS提供了很多与查找重复项相关的工作。也许你可以将UNION中的一些替换为UNION ALL,其中不会出现重复项。)