我想按用户为每个错误选择 10 个以前的日志条目:
table errors
user, timestamp, status_code
1, 2017-01-01 20:00, 404
2, 2017-01-01 18:00, 404
1, 2017-01-01 15:00, 401
table log
user, timestamp
1, 2017-01-01 19:55
2, 2017-01-01 19:55
4, 2017-01-01 19:55
1, 2017-01-01 19:54
1, 2017-01-01 19:52
...
例如用户 1 在 20:00 出现错误。因此,我想在 20:00 之前为用户 1 选择最新的 10 条日志条目:
SELECT * FROM log WHERE user = 1 AND timestamp <= 20:00 ORDER BY timestamp DESC LIMIT 10
但是,我希望对状态码为 404 的错误表中的所有条目执行此操作
最佳答案
通过使用 Left Join 您可以获取基于另一个表的值。 例如:
select a., b. from first_table_name as a Left Join second_table_name As b On a.id = b.user_id where b.timestamp <= 'value'
这将解决您的难题。
关于mysql - sql根据其他表选择时间戳之前的最新10个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46017577/