sql-两列的计数

11dmarpk  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(410)

我在数据库里放了很多行,我有:

+----+---------------------+-------------+----------------+-----------+
| ID | time                | os_name     | program        | last_line |
+----+---------------------+-------------+----------------+-----------+
| 14 | 2019-01-04 14:00:00 | titi        | roro           | 109       |
| 15 | 2019-01-04 14:00:00 | titi        | roro           | 109       |
| 16 | 2019-01-04 14:00:00 | gege        | fefe           | 109       |

我想要:

+---------------------+-------------------------+-------------+-----------+-----------+
| time                | nb_os_name_program      | os_name     | program   | last_line |
+---------------------+-------------------------+-------------+-----------+-----------+
| 2019-01-04 14:00:00 |                      2  | titi        | roro      | 109       |
| 2019-01-04 14:00:00 |                      1  | gege        | fefe      | 109       |
7y4bm7vi

7y4bm7vi1#

简单 GROUP BY 通过聚合可以帮助您:

SELECT time, os_name, count(*) as nb_os_name, program, lastline, 
FROM table t
GROUP BY time, os_name, program, lastline

相关问题