0

我正在尝试在 phpmyadmin mysql 上使用不同的 where 语句加入两个查询,以下是我需要帮助的代码:

SELECT elec.`Property ID`, elec.`Year`, elec.`Month`, elec.`Electric Consumption`, gas.`Gas Consumption`
FROM
(
SELECT `Property ID`, `Year`, `Month`, `Year_ref`, Sum(`Electric Consumption`) AS 'Electric Consumption', Sum(`Electric Cost`) AS 'Electric Cost'
FROM utility_use
WHERE `Electric Cost` > 0
GROUP BY `Property ID`, `Year`, `Month`, `Year_ref`
HAVING `Property ID`= 4
ORDER BY `Year`, `Month`;
) as elec 
INNER JOIN
(
SELECT `Property ID`, `Year`, `Month`, `Year_ref`, `Gas Consumption` AS 'Gas Consumption', Sum(`Gas Cost`) AS 'Gas Cost'
FROM utility_use
WHERE `Gas Cost`> 0
GROUP BY `Property ID`, `Year`, `Month`, `Year_ref`
HAVING `Property ID`= 4
ORDER BY `Year`, `Month`;
) as gas
ON (elec.`Year` = gas.`Year`) AND (elec.`Month` = gas.`Month`) AND (elec.`Property ID` = gas.`Property ID`)
GROUP BY elec.`Property ID`, elec.`Year`, elec.`Month`, elec.`Electric Consumption`, gas.`Gas Consumption`
ORDER BY elec.`Year`, elec.`Month`;

它给了我一个语法错误,我似乎无法弄清楚请帮忙。

4

2 回答 2

0

摆脱order by子查询中的子句。

另外,发布语法错误的内容。

于 2015-05-07T00:31:44.330 回答
0
  • 无效的 sql - Gas Consumption` 不包含在聚合函数或 GROUP BY 子句中。
  • 正确的 SQL 查询,
于 2015-05-07T00:50:00.743 回答