SQL:最近的时间戳到最大值(时间戳)

时间:2017-03-30 08:47:01

标签: mysql sql timestamp mysql-5.7 dbeaver

我正在从一个表中寻找最接近另一个表的最大日期的日期。所以最近的event.end时间戳到另一个表的max(时间戳)。目前我正在以这种方式尝试:

SELECT
  users.id as userID,
  users.created_at as AccountCreation,
  TIMESTAMPDIFF(DAY,users.created_at,max(app2xpr_analytics.logs_api_requests.timestamp))as ActiveDays,
  max(app2xpr_analytics.logs_api_requests.timestamp) as LaatsteLogin,
  max(events.`end`) as LaatsteEvent
FROM 
  users
LEFT JOIN app2xpr_analytics.logs_api_requests ON app2xpr_analytics.logs_api_requests.user_id = users.id
LEFT JOIN checkins ON checkins.id_user = users.id
LEFT JOIN events ON checkins.id_event = events.id_event
WHERE events.`end` < max(app2xpr_analytics.logs_api_requests.timestamp)
GROUP BY users.id LIMIT 10;

这不起作用(无效的群组功能)但是,我也试过

MIN(TIMESTAMPDIFF,DAY,events.`end`,max(app2xpr_analytics.logs_api_requests.timestamp)

也不起作用。

0 个答案:

没有答案