如何根据当前用户时区从mysql中检索数据

时间:2016-07-07 11:39:03

标签: mysql

我有一个存储客户订单的数据库表

现在我正在尝试根据用户每天的总订单绘制折线图,​​但由于订单日期时间以utc格式分开保存,图表不正确,我写了以下查询

SELECT count(*) AS RECORD_COUNT,cast(concat(vtiger_salesorder.purchase_date,' ', vtiger_salesorder.purchase_time) as DATETIME) AS 'SalesOrder_Purchase_Date_and_Time_Day' ,`vtiger_salesorder`.`purchase_date` FROM vtiger_salesorder inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_salesorder.salesorderid left join vtiger_groups on vtiger_groups.groupid = vtiger_crmentity.smownerid left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid WHERE vtiger_salesorder.salesorderid > 0 AND vtiger_crmentity.deleted=0 and (( vtiger_salesorder.purchase_date BETWEEN '2016-06-08 07:00:00' AND '2016-07-08 06:59:59' ) ) GROUP BY SalesOrder_Purchase_Date_and_Time_Day ORDER BY `vtiger_salesorder`.`purchase_date` ASC

例如,如果我有两个日期时间为2016-04-03 03:00:00的订单 那么对于2014-04-02它应该显示1和0为03,但它根据EST时区给出1为03和0为02

我该怎么办?

1 个答案:

答案 0 :(得分:0)

最后找到了使用CONVERT_TZ函数的解决方案

SELECT count(*) AS RECORD_COUNT,date_format( CONVERT_TZ( cast(concat(vtiger_salesorder.purchase_date,' ',vtiger_salesorder.purchase_time) as DATETIME), 'GMT','America/Los_Angeles') , '%D %b %Y') AS 'SalesOrder_Purchase_Date_and_Time_Day' ,`vtiger_salesorder`.`purchase_date` FROM vtiger_salesorder inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_salesorder.salesorderid left join vtiger_groups on vtiger_groups.groupid = vtiger_crmentity.smownerid left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid WHERE vtiger_salesorder.salesorderid > 0 AND vtiger_crmentity.deleted=0 and (( vtiger_salesorder.purchase_date BETWEEN '2016-06-08 07:00:00' AND '2016-07-08 06:59:59' ) ) GROUP BY SalesOrder_Purchase_Date_and_Time_Day ORDER BY `vtiger_salesorder`.`purchase_date` ASC