MySQL/PHP get average of past two months based on variable month -


i'm trying average of value past 2 months, not based on curdate()/now(). dependent on month user looking @ in application:

ex: if i'm looking @ may, want average of april , march. if i'm looking @ february, want average of january , december (of previous year).

i have function accepts month , year of page user on (it accepts emp_id, irrelevant question).

public function getprotectedamt($month,$year,$id){ $query = "select avg(total_payout) avg_payout saved_plan_data emp_id = '$id' , //this dont know query for"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); return $row['avg_payout']; } 

in table saved_plan_data, there fields plan_month , plan_year store int values of month/year employee's total_payout saved for.

how write clause avg previous 2 months, depending on values of $month , $year are?

i think easier , more readable solution one:

select ... ...     (plan_year, plan_month) in ((2012, 12), (2013, 1)) 

you need compute appropriate values.

this quite readable:

where concat_ws('-', plan_year, plan_month) in ('2012-12', '2013-01') 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -