【发布时间】:2020-11-10 04:30:32
【问题描述】:
select name,
(select sum(balance) from customers group by name having balance>0 and `type of contract`!="loan") as holdings,
(select sum(balance) from customers group by name having balance<0 or `type of contract`="loan") as borrowings,
(select case when holdings-borrowings>0 then "positive" else "negative" end from customers) as `positive/negative`,
holdings-borrowings as total
from customers
group by name
order by name;
错误代码:1054。“字段列表”中的“馆藏”列未知。
表定义为,name varchar,type of contractvarchar,balance int。
我知道错误出在哪里,我无法使用子查询中的列别名,但我不知道如何在另一种方法中执行查询。
【问题讨论】:
-
正确的解决方案 - 将您的查询转换为 CTE 或子查询。姑息疗法 - 使用用户定义的变量。
标签: mysql sql database subquery multiple-columns