在 mysql 中使用 select 语句除法

Division using select statement in mysql

我正在尝试划分两个 select 查询,但我在除数中遇到问题,抛出 "Error Code: 1242. Subquery returns more than 1 row"

这就是我的声明的样子

下面的语句工作正常,给我一个按 col2 分组的求和值列表:

(select sum(Tab1.col1) from Tab1 where
 Tab1.col2 in(select Tab1.col2 from Tab1 join Tab2 on Tab2.col2=Tab1.col2 ) 
 and Tab1.col3='general_consumption' group by Tab11.col2);

然而,当我使用它时,上面的查询如下所示:

(Select (Tab1.Col4)from Tab1) /(select sum(Tab1.col1) from Tab1 where
 Tab1.col2 in(select Tab1.col2 from Tab1 join Tab2 on Tab2.col2=Tab1.col2 ) 
 and Tab1.col3='general_consumption' group by Tab11.col2);

我收到此错误:

Error Code: 1242. Subquery returns more than 1 row

如果我可以用任何其他方式使用它,请告诉我。

这就是问题

Select (Tab1.Col4)from Tab1

它正在返回所有行

正确的语法是这样的:

Select (Tab1.Col4)/ (select sum(Tab1.col1) from Tab1 where Tab1.col2 in(select Tab1.col2 from Tab1 join Tab2 on Tab2.col2=Tab1.col2 ) and Tab1.col3='general_consumption' group by Tab1.col2) from Tab1;