php - How can I simplify this mysql query? -


i have these 4 queries repeated 3 times each agent. there anyway simplify/combine these queries? don't mind using while loop sums. thing changes dates.

$john_week_total  = mysql_result(mysql_query("select sum(tp) info type='life' , date >= '$monday' , rvp ='john smith'"),0); $john_month_total = mysql_result(mysql_query("select sum(tp) info type='life' , date >= '$this_month' , rvp ='john smith'"),0); $john_year_total  = mysql_result(mysql_query("select sum(tp) info type='life' , date >= '$this_year' , rvp ='john smith'"),0); $john_total       = mysql_result(mysql_query("select sum(tp) info type='life' , rvp ='john smith'"),0); 

you can have multiple sum aggregators in field list

select     sum(if(date >= '$monday' , rvp = 'john smith'), tp, 0) john_week_total     sum(if(date >= '$this_month' , rvp = 'john smith'), tp, 0) john_month_totalo     -- etc.     info     type = 'life' 

your code vulnerable injection. should use parameterized queries pdo or mysqli


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

mysql - Pass value between different pages (form) with PHP -