简体   繁体   中英

How can I write this multiple-count loop as a single query?

I'm really stumped here, I've tried using:

sum(case when date_format(from_unixtime(l.date_updated), '%Y-%m-%d') = date_format(now(), '%Y-%m-%d') then 1 else 0 end) AS day0_leads,

in my query, and it did not work as intended, so I ended up using this:

    <?php

    $total_days = '14';     

    for ($i = $total_days - 1; $i >= 0; $i--)
    {
        $day = strtotime('-'.$i.' days');
        $day_string = date('n/j', $day);

        $leads = mysql_result(mysql_query("select count(*) from `leads` where date_format(from_unixtime(`date_updated`), '%m-%d-%Y') = date_format(from_unixtime($day), '%m-%d-%Y')"), 0);
        $assigns = mysql_result(mysql_query("select count(*) from `assigns` where date_format(from_unixtime(`date_assigned`), '%m-%d-%Y') = date_format(from_unixtime($day), '%m-%d-%Y') and `id_dealer` not in (1,2,3)"), 0);

        echo "['$day_string', $leads, $assigns]";

        if ($i > 0)
            echo ',';
    }

    ?>

It is making the page load slow, obviously due to unnecessary queries. What is the proper way of writing this as a single query and outputting the results? Like I said, I've tried a sum with a then else, and it did not product the correct numbers.

Any help would greatly be appreciated.

Heres my solution:

    $total_days = '14';

    // get leads count array

    $sql = mysql_query("select count(*) as `count`, `date_updated` 
                        from `leads` 
                        where date_format(from_unixtime(`date_updated`), '%Y-%m-%d') >= date_format(now() - interval $total_days day, '%Y-%m-%d')
                        group by date(from_unixtime(`date_updated`));") or die(mysql_error());

    $leads_count = array();

    while ($row = mysql_fetch_assoc($sql))
        $leads_count[date('n/j', $row['date_updated'])] = $row['count'];

    // get assigns count array

    $sql = mysql_query("select count(*) as `count`, `date_assigned` 
                        from `assigns` 
                        where date_format(from_unixtime(`date_assigned`), '%Y-%m-%d') >= date_format(now() - interval $total_days day, '%Y-%m-%d') and `id_dealer` not in (1,2,3,4)
                        group by date(from_unixtime(`date_assigned`));") or die(mysql_error());

    $assigns_count = array();

    while ($row = mysql_fetch_assoc($sql))
        $assigns_count[date('n/j', $row['date_assigned'])] = $row['count'];

    for ($i = $total_days - 1; $i >= 0; $i--)
    {
        $day = strtotime('-'.$i.' days');
        $day_string = date('n/j', $day);

        $leads = ((empty($leads_count[$day_string])) ? '0' : $leads_count[$day_string]);
        $assigns = ((empty($assigns_count[$day_string])) ? '0' : $assigns_count[$day_string]);

        echo "['$day_string', $leads, $assigns]";

        if ($i > 0)
            echo ',';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM