简体   繁体   English

mysql查询时区转换

[英]mysql Query Timezone conversion

I have show times stored in all in Eastern and ID from timezone settings table. 我已将显示时间全部存储在东部和时区设置表中的ID中。 Timezone settings table has Timezone_id, GMT offset and the timezone Name. 时区设置表包含Timezone_id,GMT偏移量和时区名称。

GMT offset are stored like +6, -4, +3.5, -4.5, etc. GMT偏移量存储为+ 6,-4,+ 3.5,-4.5等。

I am trying to write a query to pull the showtime converted to its original timezone. 我正在尝试编写查询以将转换为原始时区的放映时间拉出。 I am doing like this. 我就是这样

SELECT Date_format( CONVERT_TZ( CONVERT_TZ(A.START_TIME,  '+00:00',  '-5:00'),  
'+00:00',     CONCAT(B.GMT_OFFSET , ':00' )), '%Y-%m-%e %r:%i')  
AS  'start_time' from shows A, tz_settings B AND <<JOINS>>;

What I am doing here is first to convert the time from ET to GMT and then applying the GMT offset. 我在这里要做的是首先将时间从ET转换为GMT,然后应用GMT偏移量。

START_TIME is in date format like '2012-4-23 10:15:00' Also, one more problem is converting formats like "3.5" to "3.30" to pass to Convert_TZ START_TIME的日期格式为“ 2012-4-23 10:15:00”,此外,另一个问题是将“ 3.5”格式转换为“ 3.30”格式以传递给Convert_TZ

EDIT: Table structure. 编辑:表结构。 Main fields. 主要领域。

   shows: 'id', 'show_name', 'stat_time', 'tz_id'
   tz_settings: 'tz_id', 'gmt_offset','tz_name'

Any ideas? 有任何想法吗? Any other functions would help in the mix? 还有其他功能可以帮助混合吗?

Don't really know what is the problem that you are having, but I just build 2 MYSQL tables with your structure: 真的不知道您遇到的问题是什么,但是我只是用您的结构构建了2个MYSQL表:

MySQL Tables MySQL表

TABLE shows ['id', 'show_name', 'start_time', 'tz_id']
TABLE tz_settings ['tz_id', 'gmt_offset','tz_name']

And with this MySQL query: 并使用此MySQL查询:

SELECT Date_format( CONVERT_TZ( CONVERT_TZ( A.START_TIME, '+00:00', '-5:00' ) , '+00:00', CONCAT( B.GMT_OFFSET, ':00' ) ) , '%Y-%m-%e %r:%i' ) AS 'start_time'
FROM shows A, tz_settings B
WHERE A.id =1
AND B.tz_id = A.tz_id

The result was as expected! 结果符合预期!


Regardless, You should be storing the GMT OFFSET with it's full format to simplify your query thus losing the CONCAT and improving performance. 无论如何,您应该以完整格式存储GMT OFFSET以简化查询,从而丢失CONCAT并提高性能。 Ex.: instead of +5, store +05:00 例如:储存+05:00而不是+5

Does this helps you in any way ? 这对您有任何帮助吗?


EDITED TO INCLUDE MYSQL IF AND REPLACE 编辑以包括MYSQL IF和替换

SELECT Date_format( CONVERT_TZ( CONVERT_TZ( A.STAT_TIME, '+00:00', '-5:00' ) , '+00:00', if( B.GMT_OFFSET LIKE '%.5', REPLACE( B.GMT_OFFSET, '.5', ':30' ) , CONCAT( B.GMT_OFFSET, ':00' ) ) ) , '%Y-%m-%e %r:%i' ) AS 'start_time'
FROM shows A, tz_settings B
WHERE A.`id` =1
AND B.`tz_id` = A.`tz_id`

So, if the stored GMT OFFSET as .5 on it, it will be replaced by :30, otherwise, it will append the :00 to the existent value. 因此,如果将存储的GMT OFFSET设置为.5,则它将被:30替换,否则,它将:00附加到现有值之后。 (Tested and returns the value as expected) (经过测试并按预期返回值)

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

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