gpt4 book ai didi

mysql - 使 MySQL 查询使用 group by 语句选择最大时间戳

转载 作者:行者123 更新时间:2023-11-28 23:11:15 26 4
gpt4 key购买 nike

这是我的查询:

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

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

+--------------------+------------+------ ----------+
|日志时间戳 |系统名称 |派系名称 |
+--------------------+------------+------------ -----+
| 2017-08-14 11:18:46 |凪井 | Nagii专制|
| 2017-08-14 12:38:03 |凪井 | Nagii专制|
| 2017-08-14 13:40:18 |凪井 | Nagii专制|
| 2017-08-15 06:09:52 |凪井 | Nagii专制|
| 2017-08-14 11:18:46 |凪井 |凪井网络 |
| 2017-08-14 12:38:03 |凪井 |凪井网络 |
| 2017-08-14 13:40:18 |凪井 |凪井网络 |
| 2017-08-15 06:09:52 |凪井 |凪井网络 |
| 2017-08-14 11:18:46 |凪井 |凪井队 |
| 2017-08-14 12:38:03 |凪井 |凪井队 |
| 2017-08-14 13:40:18 |凪井 |凪井队 |
| 2017-08-15 06:09:52 |凪井 |凪井队 |
| 2017-08-14 11:18:46 |凪井 |凪井联盟 |
| 2017-08-14 12:38:03 |凪井 |凪井联盟 |
| 2017-08-14 13:40:18 |凪井 |凪井联盟 |
| 2017-08-15 06:09:52 |凪井 |凪井联盟 |
| 2017-08-14 11:18:46 |凪井 | Nagii联合公司 |
| 2017-08-14 12:38:03 |凪井 | Nagii联合公司 |
| 2017-08-14 13:40:18 |凪井 | Nagii联合公司 |
| 2017-08-15 06:09:52 |凪井 | Nagii联合公司 |
+--------------------+------------+------------ -----+
一组 20 行(0.00 秒)

但我只想选择包含字段 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);

我做错了什么?

最佳答案

你可以试试子查询,比如

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

关于mysql - 使 MySQL 查询使用 group by 语句选择最大时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45919711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com