如何在 MySql 选择查询中将 UTC 日期转换为本地时区

2021-11-20 00:00:00 utc localtime mysql convert-tz

我在 MySql 数据库的其中一个查询中使用了这个 Where 条件.我的问题是我的表中有一个显示时间列,但该表列以 UTC 时间显示数据.我想将该显示时间列转换为本地时区.所以我如何从查询本身提供这个功能.

I am using this Where Condition in One Of my query with MySql Database.My Problem is that i have one displaytime column in my table but that table column shows the data in UTC Time.and i want to convert that displaytime column in the Local Time Zone.so how can i provide this facility from query itself.

我已经对这些东西进行了检查,因此我知道像 SELECT CONVERT_TZ() 这样的东西会起作用.但它对我不起作用.

I have goggled the things and by that i knew that something like SELECT CONVERT_TZ() will work for that.but its not working for me.

这是我的查询,我需要将显示时间转换为本地时区...所以有人可以指导我吗?

Here is my query in which i need to convert displaytime to local time zone...so can anyone please guide me?

WHERE displaytime >= '2012-12-01 00:00:00'
  AND displaytime <='2013-02-22 23:59:59'
  AND ct.organizationId IN (
    SELECT t.organizationId
      FROM organization_ AS t
      JOIN organization_ AS p ON t.treePath LIKE CONCAT(p.treePath, '%')
     WHERE p.organizationId = 10707

示例数据

推荐答案

SELECT CONVERT_TZ() 将为此工作.但它对我不起作用.

SELECT CONVERT_TZ() will work for that.but its not working for me.

为什么,你得到了什么错误?

SELECT CONVERT_TZ(displaytime,'GMT','MET');

如果您的列类型是时间戳或日期应该可以使用

should work if your column type is timestamp, or date

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz

测试它是如何工作的:

SELECT CONVERT_TZ(a_ad_display.displaytime,'+00:00','+04:00');

检查时区表

SELECT * FROM mysql.time_zone;
SELECT * FROM mysql.time_zone_name;

http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

如果这些表为空,则说明您尚未初始化时区表.根据上面的链接,您可以使用 mysql_tzinfo_to_sql 程序加载时区表.请试试这个

If those tables are empty, you have not initialized your timezone tables. According to link above you can use mysql_tzinfo_to_sql program to load the Time Zone Tables. Please try this

shell> mysql_tzinfo_to_sql /usr/share/zoneinfo

或者如果不工作阅读更多:http://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html

or if not working read more: http://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html

相关文章