我正在执行一个使用连接的查询。因为我使用了一个总量的子查询。我想按子查询的结果排序,如下所示。
"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。
如何使用子查询的结果进行查询?
答案 0 :(得分:1)
我不知道是什么触发了您获得的错误,但将子选择移动到内部联接应该会产生相同的结果。
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
答案 1 :(得分:0)
试试这个
选择用户。*,(从捐赠d中选择sum(d.amount),从d.uid = u.id中选择用户u)作为来自用户的camount,其中users.confirmed = 1且camount在3到5之间按camount排序