您好,我试图通过将1的值乘以200,将另一个值乘以50来获得2行的总数。

我有一张桌子

=====================
ClubName | Type | Fee
=====================
Club1    |Adult | 1000
Club1    |Child | 500


我正在尝试将1000乘以200,将500乘以50,然后将它们相加,这就是我想要得到的总数。

我有一个成年人用的,但是我又该如何去生孩子呢?

SELECT Type, SUM(Fee * 200) AS MembershipFee
FROM fees
WHERE Clubname='hillcrest' AND Type='adult'

最佳答案

如果我对您的理解正确,那应该可以:

SELECT Type,
SUM(CASE WHEN Type = 'Adult' THEN Fee*200
         WHEN Type = 'Child' THEN Fee*50 END) AS MembershipFee
 FROM yourTable
 WHERE Clubname='hillcrest'
 Group BY Type


如果您只想获取总和,则可以执行以下操作。

SELECT Clubname,
SUM(CASE WHEN Type = 'Adult' THEN Fee*200
         WHEN Type = 'Child' THEN Fee*50 END) AS MembershipFee
FROM yourTable
WHERE Clubname='hillcrest'
Group BY Clubname

关于mysql - SQL列的总计,但2行将每个值乘以设置值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50029086/

10-11 22:22