gpt4 book ai didi

mysql - 如何从MySQL中的 View 中选择一个字段?

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:38 25 4
gpt4 key购买 nike

如何在 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

如果您要查找每个人,请添加 GROUP BY:

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 - 如何从MySQL中的 View 中选择一个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15572190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com