如何使Mysql COUNT返回每个组的计数值?

时间:2019-05-13 15:57:42

标签: mysql

我试图获取每个返回的WorkerName总数的计数。现在,我对所有WorkerName结果都得到相同的值。我是否需要在SELECT中使用子查询或可能通过JOIN使用子查询来完成此操作?

WorkerName | BlockCount
name1 | 35
name2 | 35
name3 | 35
name4 | 35

这就是我要返回的内容

WorkerName | BlockCount
name1 | 10
name2 | 5
name3 | 7
name4 | 13

SQL:

SELECT
    Worker.WorkerName AS Worker,
    COUNT(Block.BlockNum) AS BlockCount
FROM Block
    LEFT JOIN Coin ON Coin.CoinID = Block.CoinID
    LEFT JOIN Worker ON Worker.MinerID = Block.MinerID
WHERE Worker.MinerID = '615'
    AND FROM_UNIXTIME(Block.BlockTime/1000) >= DATE_SUB(NOW(), INTERVAL 30 DAY)
    AND Block.CoinID = '13'
GROUP BY Worker.WorkerName

0 个答案:

没有答案