简体   繁体   English

循环浏览2个查询的结果

[英]Looping through results of 2 queries

I have 2 queries that show month to date totals. 我有2个查询,显示本月至今的总数。 I need to loop through them so they end up in the same table. 我需要遍历它们,以便它们最终出现在同一张表中。 Is it possible to combine the WHILE or combine the queries to have the ORDERS alternate? 是否可以合并WHILE或合并查询以使ORDERS交替?

$MTDcurrent = $db->Execute("SELECT SUM(order_total) AS MTDC, month(date_purchased) AS Months FROM " . TABLE_ORDERS ." 
                        WHERE orders_status IN(1,2,3,100,101,103,105) AND date(date_purchased) BETWEEN DATE_FORMAT(current_date,'%Y-01-01') AND current_date GROUP BY Months ASC LIMIT 10 ");

$MTDprevious = $db->Execute("SELECT SUM(order_total) AS MTDP, month(date_purchased) AS Months FROM " . TABLE_ORDERS ." 
                        WHERE orders_status IN(1,2,3,100,101,103,105) AND date(date_purchased) BETWEEN DATE_FORMAT(date_sub(current_date, INTERVAL 1 year),'%Y-01-01') AND date_sub(current_date, INTERVAL 1 year)
                        GROUP BY Months ASC LIMIT 10 ");

Thanks in Advance 提前致谢

You can do this directly with while loop like this: 您可以使用while循环直接执行以下操作:

while( ($row = array_shift( $MTDcurrent)) || ($row = array_shift( $MTDprevious))){
    $row = 'your data';
}

However I don't consider this clean and obvious way. 但是我不认为这是干净明显的方法。 You may rather merge arrays with array_merge (if there's no large amount of data): 您可能希望将数组与array_merge合并(如果没有大量数据):

foreach( array_merge( $MTDcurrent, $MTDprevious) as $row){
   $row = 'your data';
}

And if your code is performance critical and will use large amount of data you should implement Iterator with next function like this: 而且,如果您的代码对性能至关重要,并且会使用大量数据,则应使用next函数实现Iterator ,如下所示:

public function next() {
  $this->row = mysql_fetch_assoc( $this->query);
  if( $this->row){
     return;
  }

  if( count( $this->queries)){
    $this->query = mysql_query( array_shift( $this->queries));
    $this->next();
  }
}

Warning: this is just an idea, not whole implementation you should rely on! 警告:这只是一个想法,而不是您应该依靠的整个实现!

EDIT blah, I got question wrong, you're looking for mysql UNION statement . 编辑等等,我问错了,您正在寻找mysql UNION语句

This is a slightly different approach avoiding the multiple queries. 这是避免重复查询的略有不同的方法。 I have not tried it as I do not have an RDBMS on the machine I am using right now but it should be close enough - 我没有尝试过,因为我现在正在使用的计算机上没有RDBMS,但它应该足够接近-

SELECT
    SUM(order_total) AS MTDP,
    month(date_purchased) AS Months,
    year(date_purchased) AS Years
FROM orders
WHERE orders_status IN(1,2,3,100,101,103,105)
AND DATE(date_purchased) > DATE_FORMAT(DATE_SUB(CURRENT_DATE, INTERVAL 1 year),'%Y-01-01')
GROUP BY Years, Months
ORDER BY Months ASC, Years ASC

In your response to your comment - you could modify the date part of the WHERE clause to be a combination of your two WHERE clauses 在回应您的评论时-您可以将WHERE子句的日期部分修改为两个WHERE子句的组合

AND (
        (date(date_purchased) BETWEEN DATE_FORMAT(current_date,'%Y-01-01') AND current_date)
    OR
        (date(date_purchased) BETWEEN DATE_FORMAT(date_sub(current_date, INTERVAL 1 year),'%Y-01-01') AND date_sub(current_date, INTERVAL 1 year))
)

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

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