2011-01-10 11 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

= 1とcamount users.confirmedユーザからcamountこれ

選択ユーザー。*、(寄付dから和(d.amount)を選択し、uはd.uid = u.idユーザー)してみてくださいcamountで3から5の間で注文します。

+0

ない全くOPが何を望んで。問合せは、*各レコードの* all *ユーザーの合計量を取得します。 –

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 
関連する問題