2012-03-07 84 views
2

所以我在那裏的日期和時間被分割,而不是保存爲一個單一的日期時間每小時統計的健康表:mysql:在where子句比較中忽略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 | 
+-------+-------------+------------+----------+---------------------+ 

它似乎認爲它是仰視時區調整值(「本」列),但返回的統計數據是完全相同的那些從非timezoned查詢!這裏發生了什麼?

回答

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。

+0

看起來就是這樣,感謝您的幫助。時區是屁股疼痛的時刻。不幸的是,我無法控制db架構,所以我無法合併這些字段。 – Amalgovinus 2012-05-01 01:27:35