我想显示来自mysql数据库的记录,该记录计算某列的数据并总结金额
我有一个SQL查询,我们将专注于MatchID 135
SELECT `BetType`,
`BetAmount`,
`Payout`
FROM `betdb`
LEFT JOIN `matchdb` ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
返回
之类的数据
我想列出BetTypes及其Count,BetAmt Total和Payout Total返回行类似
BetType Count BetAmount Total Payout Total
Handi 2 60000 950000
Homerun Count 4 10000 0
Total 6 70000 950000
我尝试将DISTINCT用于BetType,但它只返回第一行
答案 0 :(得分:2)
您正在寻找GROUP BY
以及相对汇总COUNT()
和SUM()
函数:
SELECT
BetType,
COUNT(*) AS 'Count'
SUM(BetAmount) AS 'BetAmount Total',
SUM(Payout) AS 'Payout Total'
FROM
betdb
LEFT JOIN matchdb
ON betdb.MatchId = matchdb.MatchID
WHERE
betdb.MatchID = 135
GROUP BY
BetType
这会将每个BetType
放入一行,并允许您应用COUNT(*)
等聚合来获取“与此BetType
匹配的行数”或使用{{1} }添加与指定的SUM(BetAmount)
匹配的所有BetAmount
个值。
答案 1 :(得分:1)
试试这个
SELECT `BetType`,
count(`BetType`) AS COUNT,
sum(`BetAmount`) AS BetAmountTotal,
sum(`Payout`) AS PayoutTotal
FROM `betdb`
LEFT JOIN `matchdb` ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
GROUP BY `BetType`
答案 2 :(得分:0)
试试:
SELECT `BetType`,count(`BetAmount`) count,sum(`BetAmount`) BetAmount_Total ,Sum(`Payout`) Payout_Total
FROM `betdb`
LEFT JOIN `matchdb` ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
GROUP BY `BetType`
答案 3 :(得分:0)
这可以使用group by和聚合函数来完成,因为你想要总计它需要汇总而在查询中有一点点变化。
select
ifnull(BetType,'Total') as BetType,
`Count`,
`BetAmount Total`,
`Payout Total`
from(
select
`BetType`,
count(*) as `Count`,
sum(BetAmount) as `BetAmount Total`,
sum(Payout) as `Payout Total`
from FROM `betdb`
LEFT JOIN `matchdb` ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
group by `BetType` with rollup
)x
答案 4 :(得分:0)
我不确定,但它认为这应该有效:
使用COUNT(*),您可以计算出Homrun Count的BetType的出现次数,这意味着4次。
使用SUM(BetAmount
),您可以汇总来自BetAmount或Payout的所有内容。
对于所有其他领域,您需要具有Group By的gro group。
SELECT `BetType`,
COUNT(*),
SUM(`BetAmount`) as 'BetAmount Total',
SUM(`Payout`) FROM `betdb`
LEFT JOIN `matchdb`
ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
GROUP BY 'BetType';
答案 5 :(得分:0)
您可以使用COUNT
进行计数,使用SUM
进行计数。像
SELECT BetType,
count(BetTypeAS BetTypeCount,
sum(BetAmount) AS BetAmountSum,
sum(PayoutAS PayoutSum
FROM betdb
LEFT JOIN matchdb
ON betdb.MatchID = matchdb.MatchID
WHERE betdb.MatchID=135
group by BetType
我没有测试过这个,但肯定会给你一个起点。