Magento group by子句在日期字段中

ROB*_*BIN 5 php mysql group-by magento

我需要在指定月份的一天内获取'grand_total'的订单总数,SUM,MIN MAX和AVG.这就是我在做的事情.

$collection->getSelect()
               ->columns( 'SUM(base_grand_total) AS total' )
               ->columns( 'COUNT(*) AS orders_count' )
               ->columns( 'DATE_FORMAT(created_at, "%d") AS order_day' )
               ->columns( 'DATE_FORMAT(created_at, "%d/%m/%y") AS order_date' )
               ->columns( 'AVG(base_grand_total) AS avg_total' )
               ->columns( 'MAX(base_grand_total) AS max_total' )
               ->columns( 'MIN(base_grand_total) AS min_total' )
               ->where( 'DATE_FORMAT(created_at, "%m") = ?', $month )
               ->where( 'DATE_FORMAT(created_at, "%Y") = ?', $year )
               ->group( 'DATE_FORMAT(created_at, "%d-%m-%y")' );
Run Code Online (Sandbox Code Playgroud)

$ month是"Sep",$ year是"2014"

自定义网格系统上的上述查询的当前输出

什么销售>订单说同一时期

上面的查询说26日有3个订单,但是magento的Sales> Order网格表示只有一个订单在第26个.我猜答案在于," created_at "在DB中存储为"2014-09-04 02:50:04",但是如果我们将其格式化为"2014年9月3日下午4:50:04".

那么,任何人都可以建议如何在Collection上应用group by date子句.

提前致谢.

β.ε*_*.βε 3

这是因为 Magento 会解析数据库中的日期,并将它们设置在 下设置的时区中System > Configuration > General > Locale Options > Timezone。但实际上是以 GMT 时间保存数据库中的值。

但这是您可以以相同方式获取和转换的信息:

解决方案1:考虑夏令时转变,但需要正确配置MySQL服务器并正确加载时区。

要确定您的服务器上是否加载了时区,请运行此查询

select * from mysql.time_zone_name;
Run Code Online (Sandbox Code Playgroud)

如果返回时区列表,您应该可以开始(尽管其他表格可能必须正确填写,另请参阅此答案: https: //stackoverflow.com/a/15419843/2123530

如果您在此表中没有任何记录,请参阅MySQL手册以了解如何在您的服务器上加载这些信息: http: //dev.mysql.com/doc/refman/5.7/en/mysql-tzinfo-to -sql.html

然后,当一切顺利时,这应该是正确的查询:

$GMTToLocaleTZDiff = Mage::getStoreConfig('general/locale/timezone',0);

$collection->getSelect()
               ->columns( 'SUM(base_grand_total) AS total' )
               ->columns( 'COUNT(*) AS orders_count' )
               ->columns( 'DATE_FORMAT(created_at, "%d") AS order_day' )
               ->columns( 'DATE_FORMAT(created_at, "%d/%m/%y") AS order_date' )
               ->columns( 'AVG(base_grand_total) AS avg_total' )
               ->columns( 'MAX(base_grand_total) AS max_total' )
               ->columns( 'MIN(base_grand_total) AS min_total' )
               ->columns( "CONVERT_TZ(created_at,'GMT','".$GMTToLocaleTZDiff."') AS created_at" )
               ->where( 'DATE_FORMAT(created_at, "%m") = ?', $month )
               ->where( 'DATE_FORMAT(created_at, "%Y") = ?', $year )
               ->group( 'DATE_FORMAT(created_at, "%d-%m-%y")' );
Run Code Online (Sandbox Code Playgroud)

解决方案 2:由于夏令时,这仍然可能导致您需要按小时轮班

$GMTToLocaleTZDiff = Mage::getSingleton('core/locale')->storeDate()->get(Zend_Date::GMT_DIFF_SEP);

$collection->getSelect()
               ->columns( 'SUM(base_grand_total) AS total' )
               ->columns( 'COUNT(*) AS orders_count' )
               ->columns( 'DATE_FORMAT(created_at, "%d") AS order_day' )
               ->columns( 'DATE_FORMAT(created_at, "%d/%m/%y") AS order_date' )
               ->columns( 'AVG(base_grand_total) AS avg_total' )
               ->columns( 'MAX(base_grand_total) AS max_total' )
               ->columns( 'MIN(base_grand_total) AS min_total' )
               ->columns( "CONVERT_TZ(created_at,'+00:00','".$GMTToLocaleTZDiff."') AS created_at" )
               ->where( 'DATE_FORMAT(created_at, "%m") = ?', $month )
               ->where( 'DATE_FORMAT(created_at, "%Y") = ?', $year )
               ->group( 'DATE_FORMAT(created_at, "%d-%m-%y")' );
Run Code Online (Sandbox Code Playgroud)