0

2 つの異なるテーブルがあります。クエリで使用されるテーブルの構造は次のとおりです。

leads:

- id
- date_added
- website

assignments:

- id
- id_lead
- date_assigned
- website

私がやりたいことは、日付範囲に基づいて、各 Web サイトleadsの とテーブルにある行数を数えることです。assignmentsたとえば、Web サイトごと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

したがって、Web サイトごとにすべての行の合計または数を表示したいと考えています。

これは私がすでに持っているクエリですが、正しくカウントされず、結合もありません:

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>';

どんな助けでも大歓迎です。

4

1 に答える 1

0

SQL クエリのこの部分に関するヒントを提供できます。

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

代わりに次のコードを使用できます。

where `id_dealership` NOT IN (65,77,89)
于 2013-03-07T14:01:52.557 に答える