挑战MySQL查询html / PHP报告

时间:2012-11-07 19:08:23

标签: php html mysql

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

我们知道如何收集数据并存储它,并拥有PHP报告布局,但是如何构建查询则会丢失。

我们有两个MySQL表 - “访客”和“买家”。

  1. 访客表 - 访问我们网站的访问者 标识sales_rep的特定URL和许多其中一个 他们自己创造的营销代码。
  2. 买家表 - 适合那些买东西的人。
  3. 访问者可以通过以下链接访问我们的网站,例如:

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

    在“访客”表中,我们包含三个字段:  

    • SALES_REP
    •   
    • 日期
    •   
    • marketing_CODE - (在这种情况下,它将是字符串中的“code1 / code2 / code3”,但每个sales_rep可以基于其广告活动具有多个marketing_CODES;例如“code1a / codeTX / codeM2”和“market- Z /广告-123" )

    当访问者购买某物时,我们会将其添加到“买家”表中,其中包括:

    • SALES_REP
    • 日期
    • marketing_TYPE - (可以是“网址”,“引荐”或其他类型)
    • marketing_VALUE - (如果marketing_TYPE是网址,则为marketing_CODE)

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

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

    Table Example

    为了确保清晰,在上表中,在第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 个答案:

答案 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