2012-11-27 27 views
0

我有2個不同的表,繼承人了在查詢中使用的表的結構:MySQL的薩姆由日期與聯接表

leads

- id 
- date_added 
- website 

assignments

- id 
- id_lead 
- date_assigned 
- website 

我想要做的是根據日期範圍計算每個網站的leadsassignments表中的行數。例如,我需要一個today的計數,這會給我每個網站today的總行數。

我要找的日期範圍是:

Today 
Yesterday 
2 Days Ago 
3 Days Ago 
4 Days Ago 
5 Days Ago 
6 Days Ago 
7 Days Ago 
This Week 
This Month 
Last Month 
This Year 

所以我想顯示總和或每個網站的所有行數。

下面是該查詢我已經,但它並沒有正確計算,並且在它沒有加入:

select `website`, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') = date_format(now(), '%Y-%m-%d') then 1 else 0 end) AS c_day, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') = date_format(now() - interval 1 day, '%Y-%m-%d') then 1 else 0 end) AS c_yesterday, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') = date_format(now() - interval 2 day, '%Y-%m-%d') then 1 else 0 end) AS c_2_days, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') = date_format(now() - interval 3 day, '%Y-%m-%d') then 1 else 0 end) AS c_3_days, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') = date_format(now() - interval 4 day, '%Y-%m-%d') then 1 else 0 end) AS c_4_days, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') = date_format(now() - interval 5 day, '%Y-%m-%d') then 1 else 0 end) AS c_5_days, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') = date_format(now() - interval 6 day, '%Y-%m-%d') then 1 else 0 end) AS c_6_days, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') = date_format(now() - interval 7 day, '%Y-%m-%d') then 1 else 0 end) AS c_7_days, 
sum(case when YEARWEEK(FROM_UNIXTIME(`date_assigned`)) = YEARWEEK(CURDATE()) then 1 else 0 end) AS c_week, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m')= date_format(now(), '%Y-%m') then 1 else 0 end) AS c_month, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y-%m')= date_format(now() - interval 1 month, '%Y-%m') then 1 else 0 end) AS c_last_month, 
sum(case when date_format(from_unixtime(`date_assigned`), '%Y')= date_format(now(), '%Y') then 1 else 0 end) AS c_year 
from `assignments` 
where `id_dealership`!='65' and `id_dealership`!='77' and `id_dealership`!='89' 
group by `website` 
order by `website` asc 

繼承人的PHP爲此吐出出來到表格式我想查詢:

echo '<table cellpadding="10" cellspacing="1" border="0" width="100%">'; 
echo '<tr>'; 
echo '<th class="l">Website</th>'; 
echo '<th>Today</th>'; 
echo '<th>Yesterday</th>'; 
echo '<th>2 Days Ago</th>'; 
echo '<th>3 Days Ago</th>'; 
echo '<th>4 Days Ago</th>'; 
echo '<th>5 Days Ago</th>'; 
echo '<th>6 Days Ago</th>'; 
echo '<th>7 Days Ago</th>'; 
echo '<th>This Week</th>'; 
echo '<th>This Month</th>'; 
echo '<th>Last Month</th>'; 
echo '<th>This Year</th>'; 
echo '</tr>'; 

$count = 1; 
$c_day_total = 0; 
$c_yesterday_total = 0; 
$c_2_days_total = 0; 
$c_3_days_total = 0; 
$c_4_days_total = 0; 
$c_5_days_total = 0; 
$c_6_days_total = 0; 
$c_7_days_total = 0; 
$c_week_total = 0; 
$c_month_total = 0; 
$c_last_month_total = 0; 
$c_year_total = 0; 

while ($row = mysql_fetch_assoc($sql)) 
{ 
    foreach ($row as $k => $v) 
     $$k = htmlspecialchars($v, ENT_QUOTES); 

    if (!empty($website)) 
    { 
     $website = '<a href="website.php?url='.$website.'">'.$website.'</a>'; 
     //$website = '<a href="http://'.$website.'">'.$website.'</a>'; 

     echo '<tr class="'.(($count % 2) ? 'row1' : 'row2').'">'; 
     echo '<td>'.$website.'</td>'; 
     echo '<td>'.$c_day.'</td>'; 
     echo '<td>'.$c_yesterday.'</td>'; 
     echo '<td>'.$c_2_days.'</td>'; 
     echo '<td>'.$c_3_days.'</td>'; 
     echo '<td>'.$c_4_days.'</td>'; 
     echo '<td>'.$c_5_days.'</td>'; 
     echo '<td>'.$c_6_days.'</td>'; 
     echo '<td>'.$c_7_days.'</td>'; 
     echo '<td>'.$c_week.'</td>'; 
     echo '<td>'.$c_month.'</td>'; 
     echo '<td>'.$c_last_month.'</td>'; 
     echo '<td>'.$c_year.'</td>'; 
     echo '</tr>'; 

     $c_day_total = $c_day_total + $c_day; 
     $c_yesterday_total = $c_yesterday_total + $c_yesterday; 
     $c_2_days_total = $c_2_days_total + $c_2_days; 
     $c_3_days_total = $c_3_days_total + $c_3_days; 
     $c_4_days_total = $c_4_days_total + $c_4_days; 
     $c_5_days_total = $c_5_days_total + $c_5_days; 
     $c_6_days_total = $c_6_days_total + $c_6_days; 
     $c_7_days_total = $c_7_days_total + $c_7_days; 
     $c_week_total = $c_week_total + $c_week; 
     $c_month_total = $c_month_total + $c_month; 
     $c_last_month_total = $c_last_month_total + $c_last_month; 
     $c_year_total = $c_year_total + $c_year; 
     $count++; 
    } 
} 

echo '<tr class="'.(($count % 2) ? 'row1' : 'row2').'">'; 
echo '<td>Totals</td>'; 
echo '<td>'.$c_day_total.'</td>'; 
echo '<td>'.$c_yesterday_total.'</td>'; 
echo '<td>'.$c_2_days_total.'</td>'; 
echo '<td>'.$c_3_days_total.'</td>'; 
echo '<td>'.$c_4_days_total.'</td>'; 
echo '<td>'.$c_5_days_total.'</td>'; 
echo '<td>'.$c_6_days_total.'</td>'; 
echo '<td>'.$c_7_days_total.'</td>'; 
echo '<td>'.$c_week_total.'</td>'; 
echo '<td>'.$c_month_total.'</td>'; 
echo '<td>'.$c_last_month_total.'</td>'; 
echo '<td>'.$c_year_total.'</td>'; 
echo '</tr>'; 

echo '</table>'; 

任何幫助將不勝感激。

+1

什麼是date_assigned列的數據類型。並且還添加兩個表的完整創建腳本 –

回答

0

我可以給你一個關於你的SQL查詢的這部分提示:

where `id_dealership`!='65' and `id_dealership`!='77' and `id_dealership`!='89' 

您可以使用此代碼來代替:

where `id_dealership` NOT IN (65,77,89)