Every derived table must have its own alias
在做多表连接查询时会出现这个错误,解决方法:每一个表都需要一个自己的别名。
SELECT c.NAME
,c.flow,a.flowYoY,b.flowChain from
(SELECT
t1.NAME,
SUM( t2.flow ) AS flow
FROM
t_reservoir t1
LEFT JOIN t_reservoir_record t2 ON t2.reservoir_code = t1.CODE
WHERE
t1.CODE = “482f80592e6a49d488aaec196b148427”
AND YEAR ( t2.create_datetime ) = YEAR (now())
AND MONTH ( t2.create_datetime ) = MONTH (NOW())
GROUP BY
t1.NAME)c
Left JOIN
(SELECT
t1.NAME,
SUM(t2.flow) AS flowYoY
FROM
t_reservoir t1
LEFT JOIN t_reservoir_record t2 ON t2.reservoir_code = t1.CODE
WHERE
t1.CODE = “482f80592e6a49d488aaec196b148427”
AND YEAR (t2.create_datetime) = YEAR (now())-1
AND MONTH (t2.create_datetime) = MONTH (NOW())
GROUP BY
t1.NAME)a on a.name = c.name
left JOIN
(SELECT
t1.NAME,
SUM( t2.flow ) AS flowChain
FROM
t_reservoir t1
LEFT JOIN t_reservoir_record t2 ON t2.reservoir_code = t1.CODE
WHERE
IF(Month(t2.create_datetime)=1,YEAR(t2.create_datetime)=YEAR (now())-1 and MONTH(t2.create_datetime)=1,YEAR(t2.create_datetime)=YEAR (now())
AND MONTH (t2.create_datetime) = MONTH (NOW())-1) and
t1.CODE = “482f80592e6a49d488aaec196b148427”
GROUP BY
t1.NAME) b on b.name =c.name
这样每个表都有自己的别名,就解决了问题