3

因此,我正在使用该代码从 MySQL 获取一些数据:

<?
$query=mysql_query("SELECT date,COUNT(*) as num FROM downloads WHERE prjID='".$_GET['id']."' GROUP BY date ORDER BY date ASC");
$num=mysql_num_rows($query);
$res='';
$i=0;
while($row=mysql_fetch_array($query)){

$i++;
$date=date("d.m.Y", strtotime($row['date']));

$dan=date("d", strtotime($row['date']));
$mesec=date("m", strtotime($row['date']));
$leto=date("Y", strtotime($row['date']));

if($i=1){
$danPrvi=$leto.", ".($mesec-1).", ".$dan;
$dan1=date("d", strtotime(time()));
$mesec1=date("m", strtotime(time()));
$leto1=date("Y", strtotime(time()));
$danZadnji=$leto1.", ".($mesec1-1).", ".$dan1;
}

$numb=1;

if($row['num']!=1){
$res.="[Date.UTC(".$leto.",".($mesec-1).",".$dan."),".$row['num']."], ";
}
else{
if($i!=$num){
$res.="[Date.UTC(".$leto.",".($mesec-1).",".$dan."),".$numb."], ";
}
else{
$res.="[Date.UTC(".$leto.",".($mesec-1).",".$dan."),".$numb."]";
}
}
}
?>

我得到这样的结果:

1.3.2013 - 1
6.3.2013 - 5

但我想得到这样的结果:

1.3.2013 - 1
2.3.2013 - 0
3.3.2013 - 0
4.3.2013 - 0
5.3.2013 - 0
6.3.2013 - 1

我也在使用 Highcharts,所以日期值的格式必须像 Date.UTC(year, month-1, day)

编辑:

我的数据库中没有所有日期。在我的例子中,只有 1.3.2013 和 6.3.2013,那么我将如何检测并设置 0 值之间的所有日期,没有值 >=1?

4

3 回答 3

4

我想您需要做的是设置一个构造来保存您感兴趣的日期,并在查询中使用它或对查询数据进行后处理。例如:

<?php
$query=mysql_query("SELECT date,COUNT(*) as num FROM downloads WHERE prjID='".$_GET['id']."' GROUP BY date ORDER BY date ASC");
$num=mysql_num_rows($query);

// Get the first and last dates in the result set
$firstRow = mysql_result($query, 0);
$lastRow = mysql_result($query, $num-1);

// Now make thos the begin and end dates
$beginDate = new DateTime(strtotime($firstRow['date']));
$endDate = new DateTime(strtotime($lastRow['date']));
$currentDate = $beginDate;
$interestingDates = array();

// Populate our interestingDates array with all counts set to 0
while ($currentDate <= $endDate){
    $interestingDates[$currentDate->format('d.m.Y')] = 0;
    $currentDate->add(new DateInterval('P1D'));
}

// Reset the data result for looping over
mysql_data_seek($query,0);
while($row=mysql_fetch_array($query)){
    // Go ahead and format the string
    $formatedString = date("d.m.Y", strtotime($row['date']));

    // If the string is in our interestingDates array, update the count
    if (array_key_exists($formatedString, $interestingDates)){
        $interestingDates[$formatedString] = $row['num'];
    }
}

// Print it out
foreach ($interestingDates as $key=>$value){
    print "$key - $value\n";
}

注意 1: mysql_query 自 PHP 5.5.0 起已弃用,将来将被删除。请使用另一个 API - 我推荐 pdo_mysql

注 2: 当前查询未参数化。使用 PDO,这可能看起来像:

$sth = $dbh->prepare('SELECT date,COUNT(*) as num FROM downloads WHERE prjID= :prjID GROUP BY date ORDER BY date ASC');
$sth->bindParam(':prjID', $_GET['id'], PDO::PARAM_INT);
$sth->execute();

免责声明-我实际上并没有运行此代码,我只是将其写在脑海中。您可能需要对其进行测试/调试。

于 2013-03-29T14:02:19.417 回答
0

警告!在 SQL 中这样做是一种非常糟糕的风格,但如果你愿意——你可以:)

http://sqlfiddle.com/#!2/b491a/1/0

SELECT dynamic_date AS date, COUNT(downloads.date) AS num FROM (
    SELECT DATE_ADD(start, INTERVAL i - 1 DAY) AS dynamic_date, prjID FROM
      (
        SELECT @i := @i + 1 AS i FROM 
        (SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
        (SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
        (SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
        (SELECT @i:=0) init
      ) i_sequence,
      (SELECT MIN(date) start, MAX(date) finish, prjID FROM downloads WHERE prjID=5 GROUP BY prjID) minmax
    WHERE DATE_ADD(start, INTERVAL i - 1 DAY) <= finish
) date_sequence
LEFT JOIN downloads ON dynamic_date = downloads.date and downloads.prjID=date_sequence.prjID
GROUP BY dynamic_date
;
于 2013-03-29T15:42:39.190 回答
0

假设您有所有日期的数据,而不是特定 id 的数据,您可以进行条件总和:

SELECT date, sum(case when prjID='".$_GET['id']."' then 1 else 0 end) as num
FROM downloads
GROUP BY date
ORDER BY date ASC
于 2013-03-29T13:54:44.267 回答