我正在执行一个使用连接的查询。在这一点上,我使用了一个子查询,它将总计。我想按子查询的结果排序,如下所示。

"select users.*, (select sum(amount) as camount from donation where donation.uid=users.id) as camount from users where users.confirmed=1  and camount between 3 and 5 order by camount";

我得到一个错误:1064。
如何使用子查询的结果进行sotring查询?

最佳答案

我不知道是什么触发了您得到的错误,但是将子选择移动到内部连接应该会产生相同的结果。
SQL语句

select  users.*
        , donation.camount
from    users
        INNER JOIN (
          select  uid
                  , sum( amount ) as camount
          from    donation
          group by
                  uid
        ) donation ON donation.uid = users.id
where   users.confirmed = 1
        and donation.camount between 3 and 5
order by
        donation.camount

10-06 09:39