MySQL で次のクエリを実行するにはどうすればよいですか?
Select SUM(T1.Amount)
From
(
Select Distinct PersonName,Amount
From tblusers as T1
where city ="xxx"
)
MySQL で次のクエリを実行するにはどうすればよいですか?
Select SUM(T1.Amount)
From
(
Select Distinct PersonName,Amount
From tblusers as T1
where city ="xxx"
)
サブクエリのエイリアスを作成するだけです。
Select SUM(Amount)
From (
Select Distinct PersonName, Amount
From tblusers
where city ="xxx") t
また、各人を探している場合は、GROUPBYを追加します。
Select PersonName, SUM(Amount)
From (
Select Distinct PersonName, Amount
From tblusers
where city ="xxx") t
Group By PersonName
各人の合計(個別の人/金額の合計ではない)が本当に必要な場合は、サブクエリはまったく必要ありません。
Select PersonName, SUM(Amount)
From tblusers
Where city ="xxx"
Group By PersonName
ただし、実際のニーズによって異なります。
MySQLには、派生テーブルまたはサブクエリにエイリアスが必要です。したがって、次のものを使用する必要があります。
Select SUM(t1.Amount)
From
(
Select Distinct PersonName, Amount
From tblusers
where city ="xxx"
) t1
必要に応じて、次のものを使用できるはずです。
select personName, sum(Amount)
from tblusers
where city = "xxx"
group by personName
または、合計を返したいだけの場合は、次を使用できます。
select sum(Amount)
from tblusers
where city = "xxx"
group by personName
以下のクエリでそれを行いましたが、それを行う他の方法があるかどうか知りたいです。
CREATE VIEW T1 as (Select Distinct PersonName,Amount From tblusers where city ="xxx" );
Select SUM(Amount) From T1