mysql:convert_tz在where子句比较中被忽略了?

时间:2012-03-07 00:06:55

标签: mysql timezone convert-tz

所以我有一张健康的小时统计表,其中日期和时间是分开的,而不是作为单一的日期时间保存:

+-------+-------------+------------+----------+
| cakes | pies        | day        | hour     |
+-------+-------------+------------+----------+
|     1 |          28 | 2012-02-21 |       20 |
|     0 |          14 | 2012-02-21 |       21 |
|     1 |          15 | 2012-02-21 |       22 |
|     1 |          11 | 2012-02-21 |       23 |
|     0 |           7 | 2012-02-22 |        0 |
|     0 |           9 | 2012-02-22 |        1 |
|     0 |           5 | 2012-02-22 |        2 |
|     0 |           8 | 2012-02-22 |        3 |
|     1 |          11 | 2012-02-22 |        4 |
|     0 |          11 | 2012-02-22 |        5 |
|     0 |          12 | 2012-02-22 |        6 |
|     1 |          19 | 2012-02-22 |        7 |
|     0 |          26 | 2012-02-22 |        8 |
|     0 |          20 | 2012-02-22 |        9 |
|     0 |          24 | 2012-02-22 |       10 |
|     2 |          26 | 2012-02-22 |       11 |
|     1 |          22 | 2012-02-22 |       12 |
|     1 |          24 | 2012-02-22 |       13 |
|     1 |          32 | 2012-02-22 |       14 |
|     0 |          25 | 2012-02-22 |       15 |
|     2 |          20 | 2012-02-22 |       16 |
|     0 |          24 | 2012-02-22 |       17 |
|     1 |          24 | 2012-02-22 |       18 |
|     0 |          15 | 2012-02-22 |       19 |
+-------+-------------+------------+----------+

我想从这张桌子做一些时区敏感的选择,这样世界各地的人们都可以随时了解我的蛋糕或馅饼配额。

以下是我的查询没有时区转换的内容:

select cakes, pies, day, hour, 
    str_to_date(concat(day,' ',hour,':00'), '%Y-%m-%d %H:%i:%s') 'this' from stats where 
    str_to_date(concat(day,' ',hour,':00'), '%Y-%m-%d %H:%i:%s') between 
    str_to_date('2012-02-21 20:00:00', '%Y-%m-%d %H:%i:%s') and 
    str_to_date('2012-02-21 23:00:00', '%Y-%m-%d %H:%i:%s');    

..返回上面数据集的前四行:

+-------+-------------+------------+----------+---------------------+
| cakes | pies        | day        | hour     | this                |
+-------+-------------+------------+----------+---------------------+
|     1 |          28 | 2012-02-21 |       20 | 2012-02-21 20:00:00 |
|     0 |          14 | 2012-02-21 |       21 | 2012-02-21 21:00:00 |
|     1 |          15 | 2012-02-21 |       22 | 2012-02-21 22:00:00 |
|     1 |          11 | 2012-02-21 |       23 | 2012-02-21 23:00:00 |
+-------+-------------+------------+----------+---------------------+

到目前为止一切顺利。现在我需要使这个时区敏感。在加利福尼亚州说我的服务器,在夏令时期间新西兰有人试图从2012-02-21 20:00:00到2012-02-21 23:00:00访问我的蛋糕和馅饼统计数据:

select cakes, pies, day, hour, 
    str_to_date(convert_tz(concat(day,' ',hour,':00'), '+13:00','-8:00'), '%Y-%m-%d %H:%i:%s') 'this' from stats where
    str_to_date(convert_tz(concat(day,' ',hour,':00'), '+13:00','-8:00'), '%Y-%m-%d %H:%i:%s') between 
    str_to_date(convert_tz('2012-02-21 20:00:00', '+13:00','-8:00') , '%Y-%m-%d %H:%i:%s') and 
    str_to_date(convert_tz('2012-02-21 23:00:00', '+13:00','-8:00') , '%Y-%m-%d %H:%i:%s');

但是这里变得奇怪了:

+-------+-------------+------------+----------+---------------------+
| cakes | pies        | day        | hour     | this                |
+-------+-------------+------------+----------+---------------------+
|     1 |          28 | 2012-02-21 |       20 | 2012-02-20 23:00:00 |
|     0 |          14 | 2012-02-21 |       21 | 2012-02-21 00:00:00 |
|     1 |          15 | 2012-02-21 |       22 | 2012-02-21 01:00:00 |
|     1 |          11 | 2012-02-21 |       23 | 2012-02-21 02:00:00 |
+-------+-------------+------------+----------+---------------------+   

它认为它正在查找时区调整后的值(“this”列),但返回的统计数据与非timezoned查询中的统计数据完全相同!这是怎么回事?

1 个答案:

答案 0 :(得分:2)

您正在使用convert_tz将所有日期转换为比较(来自数据库的输入日期和日期),因此您不会惊讶地获得相同的数据。我想你可能想要的是

SELECT cakes, pies, day, hour, 
    CONVERT_TZ(CONCAT(day,' ',hour,':00'), '+13:00','-8:00') AS 'this' 
FROM stats 
WHERE 
    CONVERT_TZ(CONCAT(day,' ',hour,':00'), '+13:00','-8:00')
        BETWEEN 
            '2012-02-21 20:00:00' 
        AND '2012-02-21 23:00:00'

但为什么要分开存储日期和时间?这将使您的查询更容易将它们作为DATETIME字段存储在一起。此外,您不需要对已经是MySQL DATETIME值的值使用STR_TO_DATE。