我想加入2条SELECT语句。它们都可以单独正常工作,但是如果我尝试使用Union All,则会出现语法错误:
[42000] [1064]您的SQL语法有错误;查看手册 对应于您的MySQL服务器版本的正确语法, 在第29行的''附近使用
这对我没有任何意义。我搜索了该错误消息,但在该行中未使用引号(29是UNION ALL)
SELECT NULL AS id,
table_a.calendar_day AS
calendar_day,
'XXX' AS mbr_entity
,
'total' AS
product,
'total' AS
mbr_stock_category,
'total' AS component,
SUM(IF(table_a.mbr_entity = 'XXX', aim, 0)) AS aim,
SUM(IF(table_a.mbr_entity = 'XXX', cf, 0)) AS cf,
SUM(IF(table_a.mbr_entity = 'XXX', mbr, 0)) AS mbr,
SUM(IF(table_a.mbr_entity = 'XXX', ytd, 0)) AS ytd,
SUM(IF(table_a.mbr_entity = 'XXX', forecast, 0)) AS forecast,
SUM(table_b.a
+ table_b.b
+ table_b.c
+ table_b.d
+ table_b.e) AS actual
FROM table_a
left join table_b
ON table_b.calendar_day =
table_a.calendar_day
AND table_b.mbr_entity =
table_a.mbr_entity
WHERE MONTH(table_a.calendar_day) = MONTH(CURRENT_DATE())
GROUP BY calendar_day
UNION ALL
SELECT NULL AS id,
table_a.calendar_day,
table_a.mbr_entity,
'total' AS product,
'total' AS
mbr_stock_category,
'total' AS component,
SUM(IF(table_a.mbr_entity = 'XXX', 0, aim)) AS aim,
SUM(IF(table_a.mbr_entity = 'XXX', 0, cf)) AS cf,
SUM(IF(table_a.mbr_entity = 'XXX', 0, mbr)) AS mbr,
SUM(IF(table_a.mbr_entity = 'XXX', 0, ytd)) AS ytd,
SUM(IF(table_a.mbr_entity = 'XXX', 0, forecast)) AS forecast,
SUM(table_b.a
+ table_b.b
+ table_b.c
+ table_b.d
+ table_b.e) AS actual
FROM table_a
left join table_b
ON table_b.calendar_day =
table_a.calendar_day
AND table_b.mbr_entity =
table_a.mbr_entity
WHERE table_a.mbr_entity <> 'XXX'
AND MONTH(table_a.calendar_day) = MONTH(CURRENT_DATE())
GROUP BY calendar_day,
mbr_entity
答案 0 :(得分:0)
尝试再次使用代码
( SELECT NULL AS
id,
table_a.calendar_day AS calendar_day,
'XXX' AS mbr_entity,
'total' AS product,
'total' AS mbr_stock_category,
'total' AS component,
SUM( IF ( table_a.mbr_entity = 'XXX', aim, 0 ) ) AS aim,
SUM( IF ( table_a.mbr_entity = 'XXX', cf, 0 ) ) AS cf,
SUM( IF ( table_a.mbr_entity = 'XXX', mbr, 0 ) ) AS mbr,
SUM( IF ( table_a.mbr_entity = 'XXX', ytd, 0 ) ) AS ytd,
SUM( IF ( table_a.mbr_entity = 'XXX', forecast, 0 ) ) AS forecast,
SUM( table_b.a + table_b.b + table_b.c + table_b.d + table_b.e ) AS actual
FROM
table_a
LEFT JOIN table_b ON table_b.calendar_day = table_a.calendar_day
AND table_b.mbr_entity = table_a.mbr_entity
WHERE
MONTH ( table_a.calendar_day ) = MONTH ( CURRENT_DATE ( ) )
GROUP BY
calendar_day
)
UNION ALL
(
SELECT NULL AS
id,
table_a.calendar_day,
table_a.mbr_entity,
'total' AS product,
'total' AS mbr_stock_category,
'total' AS component,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, aim ) ) AS aim,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, cf ) ) AS cf,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, mbr ) ) AS mbr,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, ytd ) ) AS ytd,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, forecast ) ) AS forecast,
SUM( table_b.a + table_b.b + table_b.c + table_b.d + table_b.e ) AS actual
FROM
table_a
LEFT JOIN table_b ON table_b.calendar_day = table_a.calendar_day
AND table_b.mbr_entity = table_a.mbr_entity
WHERE
table_a.mbr_entity <> 'XXX'
AND MONTH ( table_a.calendar_day ) = MONTH ( CURRENT_DATE ( ) )
GROUP BY
calendar_day,
mbr_entity
)