0

How to write either mysql query or two diffrent queries with different result set can be converted into one array for a single table?

I have one table t1:

date            company     product      sales
------------------------------------------------
2013-12-01      abc         a1           100
2014-12-01      abc         b1           50
2014-12-01      abc         c1           100
2014-12-01      xyz         x1           100

Query1 (based on selection of date='2014-12-01'):
--------------------------------------------------------

select company, sum(sales) as day_sales from t1 where date='2014-12-01' 
group by company;

will give:

company   day_sales
---------------------------
abc       150
xyz       100

Query2 (based on selection of date between '2014-12-01' and '2013-12-31'):
--------------------------------------------------------

select company, sum(sales) as previous_year_month_sale from t1 
where date between '2013-12-01' and '2013-12-31' group by company;

will give:

company   previous_year_month_sale
-----------------------------------
abc       100

How can i combine both results into one table like:

company   day_sales   previous_year_mtd
-----------------------------------------
abc       150         100
xyz       100           0

Please suggest?

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
NKR
  • 73
  • 1
  • 8

1 Answers1

0
$sales = array();


In the query 1 results loop add this:

$sales $row['company'][0] = $row['day_sales'] ;


In the query 2 results loop add this:

$sales $row['company'][1] = $row['previous_year_month_sale'] ;


Output

foreach ($sales as $company => $val){
  echo "$company  $val[0]    $val[2] \n";
}
Misunderstood
  • 5,534
  • 1
  • 18
  • 25