我有下表,并试图计算每个用户执行的操作数.
----------------------------------
| ID | User | Action |
----------------------------------
| 1 | BobDoe | View |
| 2 | BobDoe | Edit |
| 3 | JaneDoe | Comment |
| 4 | BobDoe | Comment |
| 5 | JohnSmith | Edit |
| 6 | JaneDoe | Edit |
| 7 | JohnSmith | Comment |
| 8 | BobDoe | View |
----------------------------------
Run Code Online (Sandbox Code Playgroud)
目前我使用以下查询来获取编辑的数量,但是我想要更改它以便计算注释和视图并将它们显示在它们自己的列中,我不知道我将如何单独计算它们无需进行全新的查询.
SELECT Type, User, COUNT(*) AS Num FROM some_database GROUP BY User
有任何想法吗?
试试这个在MySQL中有效的查询,因为TRUE它相当于1,FALSE相当于0:
SELECT
User,
COUNT(*) AS Num,
SUM(Action = 'Comment') AS NumComments,
SUM(Action = 'View') AS NumViews
FROM some_table
GROUP BY User
Run Code Online (Sandbox Code Playgroud)
结果:
User Num NumComments NumViews ------------------------------------- BobDoe 4 1 2 JaneDoe 2 1 0 JohnSmith 2 1 0
| 归档时间: |
|
| 查看次数: |
7521 次 |
| 最近记录: |