2011-01-10 15 views
1

我正在執行一個查詢,我在其中使用連接。在這我已經使用了一個將總量的子查詢。我想按照下面的子查詢的結果進行排序。mysql排序和使用子查詢之間

"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查詢?

回答

0

嘗試這個

選擇用戶。*,(選擇從捐贈d總和(d.amount),用戶Ù其中d.uid = u.id),如從用戶camount其中users.confirmed = 1和camount在3到5之間的訂單由Camount

+0

不完全是什麼OP希望。查詢獲得*每個*記錄的* all *用戶總數。 – 2011-01-10 14:33:00

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