這是我對你的查詢的建議。
首先,我需要根據日期字段,找出週數,所以我用在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
無論你在這裏做什麼,那就[逃脫適當的SQL]受益於一些(http://bobby-tables.com/php )。將'$ _POST'值直接注入到查詢中是一個**非常糟糕的IDEA **。 – tadman
感謝您的信息。我剛開始學習防止SQL注入。當變量實際上是在PHP中創建的,我們將使用mysql_real_escape_string函數來保護數據。該帖子來自一個下拉菜單,所以這應該足以保證它,對吧? – furosiandragon
儘管如此,您必須對任何和所有值使用'mysql_real_escape_string'。確保你擁有每個人都是一個挑戰,這就是爲什麼使用[PDO](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo -for-database-access /)而不是已棄用的'mysql_query'界面。請務必使用[SQL注入測試工具](http://sqlmap.org/)測試您的應用程序,以確保您沒有完全暴露。 – tadman