Mysql SQL聚合子查询

Mysql SQL聚合子查询,mysql,sql,function,Mysql,Sql,Function,我想询问那些财富少于最富有者一半的人。因此,我提出了以下问题: select P.name from (select sum(B.balance) / 2 as Balance from Person P1, BankAccount B, AccountOf A where P.id = A.person_id and A.account_id = B.id group by P1.id) as X, Persons P, BankAccount B, Accou

我想询问那些财富少于最富有者一半的人。因此,我提出了以下问题:

select P.name
from
    (select sum(B.balance) / 2 as Balance
    from Person P1, BankAccount B, AccountOf A
    where P.id = A.person_id and A.account_id = B.id
    group by P1.id) as X, Persons P, BankAccount B, AccountOf A
    
where  
group by P.id
having sum(B.balance) < max(X.Balance) 
有人能解释一下我做错了什么吗?在我看来,正常查询中出现了一些错误,因为单独完成的子查询给出了正确的数量

询问那些财富少于最富有者财富一半的人

然后使用您的查询:

select P.name
from
    (select sum(B.balance) as Balance
    from Person P1, BankAccount B, AccountOf A
    where P.id = A.person_id and A.account_id = B.id
    group by P1.id) as X, Persons P, BankAccount B, AccountOf A
    
where  ...
group by P.id
having sum(B.balance) < max(X.Balance)/2

ANSI-92是使用显式联接语法替换隐式联接的标准。那已经快30岁了。请停止使用来自错误世纪的过时且容易出错的语法。这样做将有助于您在第一时间获得正确的查询,然后也更容易调试。例如,外部查询上没有连接谓词,如果使用连接语法,这是不可能发生的意外。@MatBailie感谢man的响应。我在大学里学习。但是我会调查的,谢谢。谢谢。我会试试这个。谢谢你的努力!由于我是在大学里这样做的,我认为子查询部分是不允许的,因为它给出了错误使用组函数的错误。有没有类似于我在原始问题中尝试的方法来解决这个问题?我在查询中没有看到任何语法错误,您使用的是哪种dbms?即使您的查询中有一个带有GROUPBY的子查询,您的SQL语法中也有一个错误;请查看与MySQL服务器版本对应的手册,以了解第5行“sumB.balance as balance,maxsumB.balance over richestbalanc”附近使用的正确语法。这是我得到的错误,但这个系统可能非常古老,因为问题来自:是6年前,也有同样的问题@Eshirvanaha,您使用的是哪个版本的mysql?
select P.name
from
    (select sum(B.balance) as Balance
    from Person P1, BankAccount B, AccountOf A
    where P.id = A.person_id and A.account_id = B.id
    group by P1.id) as X, Persons P, BankAccount B, AccountOf A
    
where  ...
group by P.id
having sum(B.balance) < max(X.Balance)/2