获取MySQL中不同DateTime范围值的计数
我的MySQL数据库中有两个表:
users_metrics
id | user_id | login_datetime | logout_datetime |
--------------------------------------------------.-----|
1 | 1 |2022-05-04 00:25:16 |2022-05-04 00:30:16|
-----------------------------------------------.--------|
2 | 3 |2022-05-04 10:29:16 |2022-05-04 10:40:16|
--------------------------------------------------------|
3 | 1 |2022-05-04 11:48:16 |2022-05-04 11:49:56|
--------------------------------------------------------|
4 | 3 |2022-05-04 11:58:16 |2022-05-04 12:20:16|
--------------------------------------------------------|
5 | 1 |2022-05-04 16:28:16 |2022-05-04 17:29:56|
--------------------------------------------------------|
files_uploaded
id | type | file_upload_datetime | user_id |
-----------------------------------------------
1 | csv |2022-05-04 00:29:16 | 1 |
-----------------------------------------------
2 | csv |2022-05-04 10:39:16 | 3 |
-----------------------------------------------
3 | txt |2022-05-04 11:49:16 | 1 |
-----------------------------------------------
4 | txt |2022-05-04 11:59:16 | 3 |
-----------------------------------------------
5 | csv |2022-05-04 12:09:16 | 3 |
-----------------------------------------------
6 | txt |2022-05-04 12:19:16 | 3 |
-----------------------------------------------
7 | txt |2022-05-04 16:29:16 | 1 |
-----------------------------------------------
8 | csv |2022-05-04 16:39:16 | 1 |
-----------------------------------------------
9 | txt |2022-05-04 16:49:16 | 1 |
-----------------------------------------------
10 | csv |2022-05-04 17:29:16 | 1 |
-----------------------------------------------
users_metrics
表保存某个用户的登录时间和注销时间以及files_uploaded
表记录文件文件记录由用户上传。 我正在寻找一个查询,该查询允许在第一表确定的DateTime范围内获取上载文件计数。 这将是我要寻找的结果的看法。
id | user_id | login_datetime | logout_datetime | total_files_uploaded
--------------------------------------------------.-----|---------------------|
1 | 1 |2022-05-04 00:25:16 |2022-05-04 00:30:16| 1
-----------------------------------------------.--------|---------------------|
2 | 3 |2022-05-04 10:29:16 |2022-05-04 10:40:16| 1
--------------------------------------------------------|---------------------|
3 | 1 |2022-05-04 11:48:16 |2022-05-04 11:49:56| 1
--------------------------------------------------------|---------------------|
4 | 3 |2022-05-04 11:58:16 |2022-05-04 12:20:16| 3
--------------------------------------------------------|---------------------|
5 | 1 |2022-05-04 16:28:16 |2022-05-04 17:29:56| 3
--------------------------------------------------------|---------------------|
I have two tables in my MySQL database:
users_metrics
id | user_id | login_datetime | logout_datetime |
--------------------------------------------------.-----|
1 | 1 |2022-05-04 00:25:16 |2022-05-04 00:30:16|
-----------------------------------------------.--------|
2 | 3 |2022-05-04 10:29:16 |2022-05-04 10:40:16|
--------------------------------------------------------|
3 | 1 |2022-05-04 11:48:16 |2022-05-04 11:49:56|
--------------------------------------------------------|
4 | 3 |2022-05-04 11:58:16 |2022-05-04 12:20:16|
--------------------------------------------------------|
5 | 1 |2022-05-04 16:28:16 |2022-05-04 17:29:56|
--------------------------------------------------------|
files_uploaded
id | type | file_upload_datetime | user_id |
-----------------------------------------------
1 | csv |2022-05-04 00:29:16 | 1 |
-----------------------------------------------
2 | csv |2022-05-04 10:39:16 | 3 |
-----------------------------------------------
3 | txt |2022-05-04 11:49:16 | 1 |
-----------------------------------------------
4 | txt |2022-05-04 11:59:16 | 3 |
-----------------------------------------------
5 | csv |2022-05-04 12:09:16 | 3 |
-----------------------------------------------
6 | txt |2022-05-04 12:19:16 | 3 |
-----------------------------------------------
7 | txt |2022-05-04 16:29:16 | 1 |
-----------------------------------------------
8 | csv |2022-05-04 16:39:16 | 1 |
-----------------------------------------------
9 | txt |2022-05-04 16:49:16 | 1 |
-----------------------------------------------
10 | csv |2022-05-04 17:29:16 | 1 |
-----------------------------------------------
The users_metrics
table save the login and logout time of a certain user and the files_uploaded
table records files records uploaded by users.
I am looking for a query that allows obtaining the count of uploaded files within to the datetime range established by the first table.
This would be a view of the result I'm looking for.
id | user_id | login_datetime | logout_datetime | total_files_uploaded
--------------------------------------------------.-----|---------------------|
1 | 1 |2022-05-04 00:25:16 |2022-05-04 00:30:16| 1
-----------------------------------------------.--------|---------------------|
2 | 3 |2022-05-04 10:29:16 |2022-05-04 10:40:16| 1
--------------------------------------------------------|---------------------|
3 | 1 |2022-05-04 11:48:16 |2022-05-04 11:49:56| 1
--------------------------------------------------------|---------------------|
4 | 3 |2022-05-04 11:58:16 |2022-05-04 12:20:16| 3
--------------------------------------------------------|---------------------|
5 | 1 |2022-05-04 16:28:16 |2022-05-04 17:29:56| 3
--------------------------------------------------------|---------------------|
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以根据用户ID和登录时间加入表格,然后计算上传:
输出:
如果用户在登录过程中未上传任何文件,则将显示0。如果您不想为未上传任何文件的用户显示条目,则可以用内部加入左JOIN并删除
coalesce
。You can join the tables based on the user id and login time and then count the uploads:
Output:
If a user did not upload any files during their login, this will show 0. If you do not want to show entries for users who did not upload any files you can replace the left join with an inner join and remove the
coalesce
.