Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php sql-使用时间戳每周计数记录_Php_Mysql_Sql - Fatal编程技术网

Php sql-使用时间戳每周计数记录

Php sql-使用时间戳每周计数记录,php,mysql,sql,Php,Mysql,Sql,我有一个名为“tasaciones”的表,它有两列“record”和“fecha”(fecha是时间戳) 我需要每周做一次报告,从第一次记录的日期到当前时间,每周统计一次记录。 我发现了类似的东西,但即使这是我需要的,我也不知道如何在echo数据中写入“while” SELECT year(fecha), datepart(week, fecha), count(*) FROM tasaciones GROUP BY year(fecha), datepart(week, fecha) 我希望

我有一个名为“tasaciones”的表,它有两列“record”和“fecha”(fecha是时间戳)

我需要每周做一次报告,从第一次记录的日期到当前时间,每周统计一次记录。 我发现了类似的东西,但即使这是我需要的,我也不知道如何在
echo
数据中写入“while”

SELECT year(fecha), datepart(week, fecha), count(*)
FROM tasaciones
GROUP BY year(fecha), datepart(week, fecha)
我希望得到如下结果:

SELECT year(fecha), datepart('week',fecha), count(*)
FROM tasaciones
GROUP BY year(fecha), datepart('week',fecha)
WHERE fecha <= timestamp(now())
第1周:15张唱片

第2周:10张唱片

第3周:25张唱片


第4周:25条记录

关于问题的第一部分,您的查询应该如下所示:

SELECT year(fecha), datepart('week',fecha), count(*)
FROM tasaciones
GROUP BY year(fecha), datepart('week',fecha)
WHERE fecha <= timestamp(now())
选择年份(fecha)、日期部分('week',fecha)、计数(*)
来自塔萨西翁
按年份分组(fecha),日期部分(“周”,fecha)
其中fecha在MySQL中没有
DATEPART()
。。您可以使用。所以您的查询应该是这样的

SELECT count(*) as numRecords, YEARWEEK(fecha) as weekNum
FROM table
GROUP BY YEARWEEK(fecha)
回显结果取决于您使用的api

PDO: MYSQLI:
注意:我没有发布使用Mysql\uuzapi实现这一点的方法,因为该api已被弃用,并且易受sql注入和其他许多问题的影响。如果您还没有

的话,应该切换到PDOMysqli,下面是问题的代码片段

$sql = "SELECT year(fecha), datepart(week, fecha) as week, count(*) as week_total_record
FROM tasaciones
GROUP BY year(fecha), datepart(week, fecha)";

$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
   echo $row['week']." : ".$row['week_total_record'];
}

mysql中没有
DATEPART
,请尝试改用
week
。不要在计数中不必要地使用
*
,它会降低sql的速度

$query = "SELECT week(fecha) as w, count(record1) as cnt FROM tasaciones GROUP BY  week(fecha)";

    $res = $mysqli->query($query);
$table="<table><thead><th>WEEK</th><th>RECORDS</th></thead><tbody>";
    while ($results = $res->fetch_assoc()){
      $table .="<tr><td> Week:" . $results['w'] . "</td><td>" . $results['cnt'] . "records</td></tr>";
    }

 $table .="</tbody></table>";
echo $table;
$query=“选择周(fecha)作为w,按周从tasaciones GROUP(fecha)中计数(记录1)作为cnt”;
$res=$mysqli->query($query);
$table=“WEEKRECORDS”;
而($results=$res->fetch_assoc()){
$table.=“周:”.$results['w'].“.$results['cnt'].“记录”;
}
$table.=”;
echo$表;

您的查询结果是什么?您想要的结果是什么?我更新了我的问题,谢谢您的关注我更新了我的问题一次again@JaniBolkvadze我理解你的问题。您希望php代码打印查询返回的结果。mysql中没有
DATEPART
。WEEK记录打印4行,没有数字WEEK:40 84记录,WEEK:41 35记录,WEEK:42 51记录,WEEK:4375records@JaniBolkvadze那问题是什么谢谢你的代码也能用,我已经接受了John Ruddell的回答,但是你的代码也可以运行。。再次感谢您的关注Hi,您的查询打印出84 | 201540,35 | 201541,51 | 201542,75 | 201543 84+35+51+75的总和正好是table@JaniBolkvadze好的,有问题吗?第一条记录的时间戳为2015年8月28日,因此有超过4条记录weeks@JaniBolkvadze是的,当然!很高兴我能帮上忙:)还有什么问题可以问:)
$qry = '...query here...';
$results = $mysqli->query($qry);
while($row = $results->fetch_assoc()){
    echo $row['numRecords'], $row['weekNum']
}
$sql = "SELECT year(fecha), datepart(week, fecha) as week, count(*) as week_total_record
FROM tasaciones
GROUP BY year(fecha), datepart(week, fecha)";

$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
   echo $row['week']." : ".$row['week_total_record'];
}
$query = "SELECT week(fecha) as w, count(record1) as cnt FROM tasaciones GROUP BY  week(fecha)";

    $res = $mysqli->query($query);
$table="<table><thead><th>WEEK</th><th>RECORDS</th></thead><tbody>";
    while ($results = $res->fetch_assoc()){
      $table .="<tr><td> Week:" . $results['w'] . "</td><td>" . $results['cnt'] . "records</td></tr>";
    }

 $table .="</tbody></table>";
echo $table;