2

flot で折れ線グラフを作成しています。結果が得られない日を除いて、すべて動作しています。結果 0 + 日付を返す必要があります。これはmysqlで可能ですか?これが私の現在のクエリです:

$chartQuery = "SELECT count(date) as counted_leads, UNIX_TIMESTAMP(date) as time FROM enquiries WHERE visibility != 'deleted' group by date";

それとも私のphpでそれを行う必要がありますか?これが私のコードです:

<?php 
        $last_key = end(array_keys($chartResults));
        foreach ($chartResults as $item => $value)
        {
            $timestamp = round($value['time'] * 1000);

            if ($item == $last_key)
            {
                // last element
                echo '['.$timestamp.', '.htmlentities($value['counted_leads']).']';
            }
            else 
            {
                // not last element
                echo '['.$timestamp.', '.htmlentities($value['counted_leads']).'],';
            }
        }
        unset($value);
        ?>
4

2 に答える 2

2
SELECT 
     ifnull(count(date),0) as counted_leads, 
     UNIX_TIMESTAMP(date) as time 
FROM enquiries WHERE visibility != 'deleted' 
group by date

デフォルト値に null の場合に使用

于 2012-07-30T05:55:32.007 に答える
0

これがあなたが探しているものだと思います:

SELECT time, SUM(counted_leads) AS counted_leads
FROM(
        SELECT UNIX_TIMESTAMP(date) AS time, count(1) AS counted_leads
        FROM enquiries
        WHERE visibility != 'deleted'
        GROUP BY time

        UNION ALL

        SELECT a.Date AS time, 0 AS counted_leads
        FROM (
            SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Date
            FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
            CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
            CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
        ) a
        WHERE a.Date BETWEEN (SELECT MIN(date) FROM enquiries) AND (SELECT MAX(date) FROM enquiries)
) a
GROUP BY time;
于 2012-07-30T06:03:35.730 に答える