[英]How can I simplify this mysql query?
I have these 4 queries repeated 3 times for each agent. 我对每个代理重复这4次查询3次。 Is there anyway to simplify/combine these queries?
无论如何,有没有简化/组合这些查询? I don't mind using a while loop for the sums.
我不介意使用while循环求和。 The only thing that changes are the dates.
唯一更改的是日期。
$john_week_total = mysql_result(mysql_query("SELECT SUM(tp) FROM info WHERE type='life' AND date >= '$monday' AND rvp ='john smith'"),0);
$john_month_total = mysql_result(mysql_query("SELECT SUM(tp) FROM info WHERE type='life' AND date >= '$this_month' AND rvp ='john smith'"),0);
$john_year_total = mysql_result(mysql_query("SELECT SUM(tp) FROM info WHERE type='life' AND date >= '$this_year' AND rvp ='john smith'"),0);
$john_total = mysql_result(mysql_query("SELECT SUM(tp) FROM info WHERE type='life' AND rvp ='john smith'"),0);
You can have multiple SUM
aggregators in the field list 字段列表中可以有多个
SUM
聚合器
SELECT
SUM(IF(date >= '$monday' AND rvp = 'john smith'), tp, 0) AS john_week_total
SUM(IF(date >= '$this_month' AND rvp = 'john smith'), tp, 0) AS john_month_totalo
-- etc.
FROM
info
WHERE
type = 'life'
Your code is vulnerable to injection. 您的代码容易被注入。 You should use properly parameterized queries with PDO or mysqli
您应该对PDO或mysqli使用正确参数化的查询
Does a query of this sort help you? 这种查询对您有帮助吗?
select
sum(case when date >= '$monday' then tp else 0 end) as weektotal,
sum(case when date >= '$this_month' then tp else 0 end) as monthtotal,
sum(case when date >= '$this_year' then tp else 0 end) as yeartotal,
sum(tp) as alltotal
from info
where type = 'life'
and rvp = 'john smith'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.