我已经错误地发布了同样的帖子。因此,要求纠正相同的问题。在此查询中是否可以避免使用UNION?任何人都可以帮助这个查询吗?提前致谢
SELECT A.M_TITLE,
A.M_NUMBER,
B.M_NAME,
B.M_DESCRIPTION,
A.FORM_TYPE,
B.SUBMISSION_DATE,
B.CREATON_DATE,
A.R_OBJECT_ID AS FORM_ID,
B.R_OBJECT_ID AS FORMDATA_ID,
'' AS ATTACHMENTS
FROM M_FORM A,
M_FORMDATA B
WHERE A.M_NUMBER = B.M_NUMBER
AND ANY B.ATTACHMENTS IN
(SELECT ATTACHMENTS FROM M_FORMDATA C WHERE B.M_NUMBER = C.M_NUMBER)
AND ACTION_ID = 'XXXX'
AND SUBMISSION_DATE >= DATE('XXXX')
AND SUBMISSION_DATE <= DATE('XXXX')
UNION
SELECT A.M_TITLE,
A.M_NUMBER,
B.M_NAME,
B.M_DESCRIPTION,
A.FORM_TYPE,
B.SUBMISSION_DATE,
B.CREATION_DATE,
A.R_OBJECT_ID AS FORM_ID,
B.R_OBJECT_ID AS FORMDATA_ID,
'null' AS ATTACHMENTS
FROM M_FORM A,
M_FORMDATA
WHERE A.M_NUMBER = B.M_NUMBER
AND NOT ANY B.ATTACHMENTS IN
(SELECT ATTACHMENTS FROM M_FORMDATA C WHERE B.M_NUMBER = C.M_NUMBER)
AND ACTION_ID= 'XXXX'
AND SUBMISSION_DATE >= DATE('XXXX')
AND SUBMISSION_DATE <= DATE('XXXX')
答案 0 :(得分:1)
如果我正确理解了查询,唯一的区别在于如何定义ATTACHMENTS
。你可以做出有条件的:
SELECT A.M_TITLE,
A.M_NUMBER,
B.M_NAME,
B.M_DESCRIPTION,
A.FORM_TYPE,
B.SUBMISSION_DATE,
B.CREATON_DATE,
A.R_OBJECT_ID AS FORM_ID,
B.R_OBJECT_ID AS FORMDATA_ID,
(case when B.ATTACHMENTS IN (SELECT ATTACHMENTS FROM M_FORMDATA C WHERE B.M_NUMBER = C.M_NUMBER)
then ''
end) AS ATTACHMENTS
FROM M_FORM A join
M_FORMDATA B
on A.M_NUMBER = B.M_NUMBER
WHERE ACTION_ID = 'XXXX' AND
SUBMISSION_DATE >= DATE('XXXX') AND SUBMISSION_DATE <= DATE('XXXX') ;
我还更改了join
语法以使用显式连接(使用on
子句)。