简体   繁体   English

如何使用 MySQL 和 CONVERT_TZ() 获取每天的最后一个条目

[英]How to get the last entry for each day using MySQL with CONVERT_TZ()

So I'm using this code to get the last entry for each day from a database.所以我使用这段代码从数据库中获取每天的最后一个条目。

SELECT a.id, a.userid, a.jurisdiction, a.country, a.date
FROM dk_location_records AS a,
        (SELECT userid, DATE(date) AS just_date, MAX(date) AS date
            FROM dk_location_records
            GROUP BY 1, 2 --userid, DATE(date)
        ) AS b
WHERE a.userid = b.userid
AND a.date = b.date;

My question is, how can I incorporate something like: CONVERT_TZ( date, '+00:00', '+01:00' ) to get the last entry per day for a different timezone.我的问题是,我如何合并以下内容: CONVERT_TZ( date, '+00:00', '+01:00' )以获取不同时区每天的最后一个条目。 I've so far managed to use the CONVERT_TZ to simply display the converted dates.到目前为止,我已经设法使用CONVERT_TZ来简单地显示转换后的日期。 However, can you use MAX() and CONVERT_TZ() together?但是,你可以一起使用MAX()CONVERT_TZ()吗?

If you want to select a single row in an extreme, you can try it by ordering the resultset by the desired criteria and limiting the output to a single row.如果您想在极端情况下将 select 设置为单行,则可以通过按所需条件对结果集进行排序并将 output 限制为单行来尝试。 Something, perhaps, like一些东西,也许,比如

SELECT a.id, a.userid, a.jurisdiction, a.country, a.date
ORDER BY CONVERT_TZ( date, '+00:00', '+01:00') DESC
LIMIT 1

It should be fairly faster than doing a subquery table scan.它应该比进行子查询表扫描要快得多。

Although, I may be misunderstanding what you're trying to do here.虽然,我可能会误解你在这里想要做什么。 Converting the timezone won't change the order of the dates.转换时区不会改变日期的顺序。 If you'd like to search for entries within a day in another timezone, it's possible to try如果您想在另一个时区搜索一天内的条目,可以尝试

SELECT a.id, a.userid, a.jurisdiction, a.country, a.date
WHERE CAST(CONVERT_TZ(a.date, '+00:00', '+01:00') AS DATE) = CURDATE()
ORDER BY a.date DESC
LIMIT 1

Of course, modify '+01:00' (destination timezone) and CURDATE() (date you're searching for) to fit your needs.当然,修改'+01:00' (目标时区)和CURDATE() (您要搜索的日期)以满足您的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM