0

对于优秀的 MySQL 编码器来说,这是一个挑战。我们将非常感谢有关创建在 html/PHP 页面中显示的 MySQL 报告的查询的帮助。

我们知道如何收集和存储数据,并拥有 PHP 报表布局,但不知道如何构建查询。

我们有两个 MySQL 表——“visitors”和“buyers”。

  1. 访问者表 - 用于通过特定 URL 进入我们网站的访问者,该 URL 标识了 sales_rep 和他们自己创建的许多营销代码之一。
  2. 买家桌——为那些买东西的人准备的。

访问者可以通过以下链接访问我们的网站,例如:

www.sales_rep.website.com/index.php?marketing=code1/code2/code3

在“访问者”表中,我们包括三个字段:

  • 销售代表
  • 日期
  • Marketing_CODE –(在本例中为字符串中的“code1/code2/code3”,但每个 sales_rep 可以根据其广告活动有多个 marketing_CODES;例如“code1a/codeTX/codeM2”和“market-z/ad” -123")

当访客购买商品时,我们会将其添加到“买家”表中,其中包括:

  • 销售代表
  • 日期
  • Marketing_TYPE –(可以是“URL”、“推荐”或其他类型)
  • Marketing_VALUE –(如果marketing_TYPE 是URL,它将是marketing_CODE)

我们需要查看每个特定销售代表的报告,对于任何指定的月份,按营销代码排序,仅针对 marketing_TYPE == "URL"。

此报告需要显示每个 marketing_CODE 的访客和买家活动的逐周细分,如下所示:

表格示例

为清楚起见,在上表中,在第 1 周,有 8 位访问者通过“code1/code2/”的链接进入网站,其中 5 位购买了东西。

我们尝试从以下内容开始查询,但不知道该去哪里:

  1. $chosen_month = $_POST['chosen_month'];
  2. SELECT * FROM visitors INNER JOIN buyers ON sales_rep WHERE marketing_type = "URL" AND sales_rep = '$sales_rep' AND date('Y-m') = $'chosen_month' ;
  3. 使用 while 语句,我们将为找到的每个记录生成表格单元格。使用它,我们知道如何列出每条记录,但我们无法弄清楚如何显示一次非不同的记录,显示它们的计数,然后显示不同的记录。

感谢您的时间和帮助。

4

1 回答 1

1

这是我对您查询的建议。

首先,我需要根据日期字段找出周数,所以我在这个问题中使用了答案:

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
于 2012-11-07T19:52:44.523 回答