【发布时间】:2018-07-17 00:10:16
【问题描述】:
有没有什么方法可以在 MySQL 中加入两个表,并为表中每个不同的 timestamp 求和第二个表中的元素?
这是我的表结构:
这是一些示例数据:
设备表:
id userId time mac 上传下载 deltaUpload deltaDownload 1 1 2008-01-01 00:00:01 00:00:00:00:01 0 0 100 1000 2 1 2008-01-01 00:00:01 00:00:00:00:02 0 0 400 4000 3 1 2008-01-01 00:00:01 00:00:00:00:03 0 0 0 500 4 2 2008-01-01 00:00:01 10:00:00:00:00 0 0 1000 5000 5 2 2008-01-01 00:00:01 20:00:00:00:00 0 0 4000 5000 6 1 2008-01-01 00:10:01 00:00:00:00:01 100 1000 500 10000 7 1 2008-01-01 00:10:01 00:00:00:00:02 400 4000 10000 500 8 1 2008-01-01 00:10:01 00:00:00:00:03 0 500 5000 5000 9 2 2008-01-01 00:10:01 10:00:00:00:00 1000 5000 500 10000 10 2 2008-01-01 00:10:01 20:00:00:00:00 4000 5000 10000 20000用户表:
id 用户名 1 一些用户名 2 其他用户基本上我想知道的是一个行集,其中每个时间戳(在这种情况下每 10 分钟)都有一个条目,其中包含用户所有 MAC 地址的(增量)流量的总和。
此表和用户 ID = 1 的用户的示例结果
用户名时间上传下载 SomeUsername 2008-01-01 00:00:01 500 5500 SomeUsername 2008-01-01 00:10:01 15500 15500这就是我对这个查询的了解
SELECT
`users`.`username` AS 'username',
`devices`.`time` AS 'time',
`devices`.`deltaUpload` AS 'upload',
`devices`.`deltaDownload` AS 'download'
FROM
`devices`
LEFT JOIN
`users` ON (`users`.`id` = `devices`.`userId`)
GROUP BY`devices`.`time`;
而且我似乎只得到垃圾/没有结果
【问题讨论】:
-
users 表中的所有devices.userId 都可用吗?
标签: mysql join group-by timestamp