我有 2 个不同的表,这是查询中使用的表的结构:
leads
:
- id
- date_added
- website
assignments
:
- id
- id_lead
- date_assigned
- website
我想要做的是根据日期范围计算每个网站leads
的表格中有多少行。assignments
例如,我想要一个计数,它会给我每个网站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>';
任何帮助将不胜感激。