对于一个优秀的MySQL编码器来说,这是一个挑战。 我们非常感谢有关创建在html / PHP页面中显示的MySQL报告的查询的一些帮助。
我们知道如何收集数据并存储它,并拥有PHP报告布局,但是如何构建查询则会丢失。
我们有两个MySQL表 - “访客”和“买家”。
访问者可以通过以下链接访问我们的网站,例如:
www.sales_rep.website.com/index.php?marketing=code1/code2/code3
在“访客”表中,我们包含三个字段:
当访问者购买某物时,我们会将其添加到“买家”表中,其中包括:
我们需要查看针对任何指定月份的每个特定销售代表的报告,按营销代码排序,仅针对marketing_TYPE ==“URL”。
此报告需要显示每个marketing_CODE的访客和买家活动的逐周细分,如下所示:
为了确保清晰,在上表中,在第1周,8名访客通过“code1 / code2 /”链接进入网站,其中5人购买了东西。
我们尝试使用以下内容开始查询,但不知道该去哪里:
$chosen_month = $_POST['chosen_month'];
SELECT * FROM visitors INNER JOIN buyers ON sales_rep WHERE marketing_type = "URL" AND sales_rep = '$sales_rep' AND date('Y-m') = $'chosen_month' ;
感谢您的时间和帮助。
答案 0 :(得分:1)
这是我对您的查询的建议。
首先,我需要根据日期字段找出周数,所以我在this question中使用了答案:
SELECT WEEK(dateField, 5) -
WEEK(DATE_SUB(dateField, INTERVAL DAYOFMONTH(dateField) - 1 DAY), 5) + 1
我将从现在开始将上述结果称为@week,以使事情更具可读性:)
然后,我从一张桌子开始,试图看看销售代表每个月有多少访客:
select rep,
, SUM(CASE @week WHEN 1 THEN 1 ELSE 0 END) as Week1
, SUM(CASE @week WHEN 2 THEN 1 ELSE 0 END) as Week2
, SUM(CASE @week WHEN 3 THEN 1 ELSE 0 END) as Week3
, SUM(CASE @week WHEN 4 THEN 1 ELSE 0 END) as Week4
, COUNT(*) as Total
from visitors
group by rep
现在我们收到了这些信息,我们将对买家使用相同的信息,然后加入这两个子查询的结果,如下所示:
select v.rep
, v.Week1 , b.Week1
, v.Week2 , b.Week2
, v.Week3 , b.Week3
, v.Week4 , b.Week4
, v.Total, b.Total
from (
select rep,
, SUM(CASE @week WHEN 1 THEN 1 ELSE 0 END) as Week1
, SUM(CASE @week WHEN 2 THEN 1 ELSE 0 END) as Week2
, SUM(CASE @week WHEN 3 THEN 1 ELSE 0 END) as Week3
, SUM(CASE @week WHEN 4 THEN 1 ELSE 0 END) as Week4
, COUNT(*) as Total
from visitors
group by rep) as v
left join (select rep,
, SUM(CASE @week WHEN 1 THEN 1 ELSE 0 END) as Week1
, SUM(CASE @week WHEN 2 THEN 1 ELSE 0 END) as Week2
, SUM(CASE @week WHEN 3 THEN 1 ELSE 0 END) as Week3
, SUM(CASE @week WHEN 4 THEN 1 ELSE 0 END) as Week4
, COUNT(*) as Total
from visitors
group by rep) as b on v.rep = b.rep
这应该是你要找的!
以下是针对您的情况更新的相同代码:
select v.rep
, v.Week1 , b.Week1
, v.Week2 , b.Week2
, v.Week3 , b.Week3
, v.Week4 , b.Week4
, v.Total, b.Total
from (
select rep
, SUM(CASE WEEK(the_date, 5) -
WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
WHEN 1 THEN 1 ELSE 0 END) as Week1
, SUM(CASE WEEK(the_date, 5) -
WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
WHEN 2 THEN 1 ELSE 0 END) as Week2
, SUM(CASE WEEK(the_date, 5) -
WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
WHEN 3 THEN 1 ELSE 0 END) as Week3
, SUM(CASE WEEK(the_date, 5) -
WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
WHEN 4 THEN 1 ELSE 0 END) as Week4
, COUNT(*) as Total
from visitors
where sales_rep = '$sales_rep' AND date('Y-m') = $'chosen_month'
group by rep ) as v
left join (
select rep
, SUM(CASE WEEK(the_date, 5) -
WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
WHEN 1 THEN 1 ELSE 0 END) as Week1
, SUM(CASE WEEK(the_date, 5) -
WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
WHEN 2 THEN 1 ELSE 0 END) as Week2
, SUM(CASE WEEK(the_date, 5) -
WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
WHEN 3 THEN 1 ELSE 0 END) as Week3
, SUM(CASE WEEK(the_date, 5) -
WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
WHEN 4 THEN 1 ELSE 0 END) as Week4
, COUNT(*) as Total
from buyers
where marketing_type = "URL" AND sales_rep = '$sales_rep' AND date('Y-m') = $'chosen_month'
group by rep) as b on v.rep = b.rep