[英]Doing a Full Outer Join with Derived tables - mysql
I am having trouble with a full outer join on two tables, the sql i am using is as follows: 我在两个表上进行完全外部联接时遇到问题,我正在使用的sql如下:
SELECT
a.sryear,
b.reqyear,
a.mid,
b.description,
sum(a.kmq + b.rmq) as sum
FROM
(select
EXTRACT(YEAR
FROM
t.date) AS srYear,
m.materialid as mid,
m.description,
sum(km.quantity) as kmq
FROM
kit k,
kitmaterial km,
materials m,
service s,
transaction t
WHERE
t.serviceid=s.serviceid
AND s.kitid=k.kitid
AND k.kitid=km.kitid
AND km.materialid=m.materialid
GROUP BY
srYear,
m.materialid
ORDER BY
srYear,
m.materialid) as a FULL
OUTER JOIN
, (
SELECT
EXTRACT(YEAR
FROM
r.datecreated) AS reqYear,
m.materialid as mid,
m.description as description,
sum(rm.quantity) as rmq
FROM
requisitionmaterial rm,
materials m,
requisition r
WHERE
rm.materialid=m.materialid
AND r.requisitionid=rm.requisitionid
GROUP BY
reqYear,
m.materialid
ORDER BY
reqYear,
m.materialid
) as b
ON a.mid=b.mid
AND a.sryear=b.reqyear;
Any assistance rendered will be appreciated. 任何协助将不胜感激。
Thank you!! 谢谢!!
under the condition that inline view a
and b
works as you expected, there is one problem. 在内联视图
a
和b
按预期工作的条件下,存在一个问题。
SELECT
....
sum(a.kmq + b.rmq) as sum
SUM()
is aggregation function. SUM()
是聚合函数。 So can't be used without GROUP BY
. 所以没有
GROUP BY
不能使用。 did you mean this? 你是这个意思吗
SELECT
....
a.kmq + b.rmq as `sum`
otherwise, you should use GROUP BY with some column 否则,您应该在某些列中使用GROUP BY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.