使MySQL查询选择group by语句的最大时间戳

时间:2017-08-28 13:01:10

标签: mysql

这是我的疑问:

SELECT 
    log_timestamp, 
    system_name,
    faction_name,
    faction_allegiance, 
    faction_government_type, 
    current_state, 
    pending_state,
    recovering_state,
    influence AS influence,
    transactions.transaction_id
FROM `bgs`.`faction_log`
    INNER JOIN `factions` ON `factions`.`faction_id` = `faction_log`.`faction_id`
    INNER JOIN `transactions` ON `transactions`.`transaction_id` = `faction_log`.`transaction_id`
    INNER JOIN `system_log` ON `system_log`.`transaction_id` = `transactions`.`transaction_id`
    INNER JOIN `systems` ON `systems`.`system_id` = `transactions`.`associated_system_id` 
WHERE
    log_timestamp BETWEEN bgs.find_closest_tick_time(date('2017-08-14')) AND bgs.find_closest_tick_time(DATE_ADD(date('2017-08-14'), INTERVAL 1 DAY)) AND
    associated_system_id = 4 
GROUP BY 
    faction_name,
    log_timestamp,
    system_name,
    faction_allegiance, 
    faction_government_type, 
    current_state, 
    pending_state,
    recovering_state,
    influence,
    transactions.transaction_id

它工作正常,现在看起来像这样:

+---------------------+-------------+-----------------+ | log_timestamp | system_name | faction_name | +---------------------+-------------+-----------------+ | 2017-08-14 11:18:46 | Nagii | Nagii Autocracy | | 2017-08-14 12:38:03 | Nagii | Nagii Autocracy | | 2017-08-14 13:40:18 | Nagii | Nagii Autocracy | | 2017-08-15 06:09:52 | Nagii | Nagii Autocracy | | 2017-08-14 11:18:46 | Nagii | Nagii Network | | 2017-08-14 12:38:03 | Nagii | Nagii Network | | 2017-08-14 13:40:18 | Nagii | Nagii Network | | 2017-08-15 06:09:52 | Nagii | Nagii Network | | 2017-08-14 11:18:46 | Nagii | Nagii Posse | | 2017-08-14 12:38:03 | Nagii | Nagii Posse | | 2017-08-14 13:40:18 | Nagii | Nagii Posse | | 2017-08-15 06:09:52 | Nagii | Nagii Posse | | 2017-08-14 11:18:46 | Nagii | Nagii Union | | 2017-08-14 12:38:03 | Nagii | Nagii Union | | 2017-08-14 13:40:18 | Nagii | Nagii Union | | 2017-08-15 06:09:52 | Nagii | Nagii Union | | 2017-08-14 11:18:46 | Nagii | Nagii United Co | | 2017-08-14 12:38:03 | Nagii | Nagii United Co | | 2017-08-14 13:40:18 | Nagii | Nagii United Co | | 2017-08-15 06:09:52 | Nagii | Nagii United Co | +---------------------+-------------+-----------------+ 20 rows in set (0.00 sec)

但我想只选择包含字段log_timestamp的最大值的行。 (在这种情况下2017-08-15 06:09:52)。我试着用这样的东西:

HAVING transactions.log_timestamp >= max(date('2017-08-15 06:09:52'));

但它不会那样工作:

HAVING transactions.log_timestamp >= max(log_timestamp);

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您可以尝试使用子查询,例如

WHERE
    log_timestamp = (
      SELECT MAX(log_timestamp) 
      FROM `bgs`.`faction_log`
      WHERE log_timestamp BETWEEN bgs.find_closest_tick_time(date('2017-08-14')) AND bgs.find_closest_tick_time(DATE_ADD(date('2017-08-14'), INTERVAL 1 DAY)) AND associated_system_id = 4
) AND associated_system_id = 4

答案 1 :(得分:0)

这是另一个解决方案。如果您只想在选择列表的所有列中为每个不同的值设置一行,则不需要GROUP BY。您可以使用SELECT DISTINCT执行相同的操作,如下所示:

SELECT DISTINCT
    log_timestamp, 
    system_name,
    faction_name,
    faction_allegiance, 
    faction_government_type, 
    current_state, 
    pending_state,
    recovering_state,
    influence AS influence,
    transactions.transaction_id
FROM `bgs`.`faction_log`
    INNER JOIN `factions` ON `factions`.`faction_id` = `faction_log`.`faction_id`
    INNER JOIN `transactions` ON `transactions`.`transaction_id` = `faction_log`.`transaction_id`
    INNER JOIN `system_log` ON `system_log`.`transaction_id` = `transactions`.`transaction_id`
    INNER JOIN `systems` ON `systems`.`system_id` = `transactions`.`associated_system_id` 
WHERE
    log_timestamp BETWEEN bgs.find_closest_tick_time(date('2017-08-14')) AND bgs.find_closest_tick_time(DATE_ADD(date('2017-08-14'), INTERVAL 1 DAY)) AND
    associated_system_id = 4 
ORDER BY log_timestamp DESC
LIMIT 1

由于您只想要具有max log_timestamp的行,您可以按该列降序排序,并使用LIMIT仅返回结果中的第一行。