phpmyadmin mysql语法,用于从具有不同where子句的多个查询创建查询

时间:2021-08-31 00:49:07

I'm attempting to join two queries with different where statements, on phpmyadmin mysql, The following is the code i need help with:

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

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`;

It's giving me a syntax error, I can't seem to figure it out please help.

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

2 个解决方案

#1


  • Not valid sql - Gas Consumption` is not contained in an aggregate function or the GROUP BY clause.
  • 无效的sql - Gas Consumption`不包含在聚合函数或GROUP BY子句中。

  • Correct SQL query,
  • 正确的SQL查询,

#2


Get rid of your order by clauses in your subqueries.

通过子查询中的子句删除您的订单。

Also, post what the syntax error says.

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

#1


  • Not valid sql - Gas Consumption` is not contained in an aggregate function or the GROUP BY clause.
  • 无效的sql - Gas Consumption`不包含在聚合函数或GROUP BY子句中。

  • Correct SQL query,
  • 正确的SQL查询,

#2


Get rid of your order by clauses in your subqueries.

通过子查询中的子句删除您的订单。

Also, post what the syntax error says.

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