我最终得到了 PHP 中最糟糕的编写代码(它可以工作,但太可怕了!,也许你可以告诉我一个更好的方法)。我正在尝试在我的网页上显示 14 天的统计数据。我需要每天显示 4 个值(下载、上传的文件数;下载和上传的文件大小)。我有 2 个表(文件和 downloads_log),结构是:
文件 (file_name, file_size, upload_datetime) downloads_log (file_name, dl_datetime)
所以我需要以下内容:
Count all files uploaded from table `files` in specific day
Count all files downloaded from table `downloads_log` in specific day
SUM file_size in table `files` in specific day
SUM file_size in table `files` where files.file_name = downloads_log.file_name in specific day
这就是我过去 14 天所需要的。
还有我已经完成的“脚本”:
for($i = 13; $i >= 0; $i--) {
$query_downloads = mysqli_query($con, "SELECT files.file_name, files.file_size, downloads_log.dl_file_name, downloads_log.dl_datetime, COUNT(file_name), SUM(file_size) FROM files, downloads_log WHERE files.file_name = downloads_log.dl_file_name AND `dl_datetime` >= '".date("Y-m-d", strtotime("-$i day"))." 00:00:00' AND `dl_datetime` <= '".date("Y-m-d", strtotime("-$i day"))." 23:59:59'");
$result_downloads = mysqli_fetch_assoc($query_downloads);
$query_uploads = mysqli_query($con, "SELECT file_name, file_size, COUNT(file_name), SUM(file_size) FROM files WHERE `upload_datetime` >= '".date("Y-m-d", strtotime("-$i day"))." 00:00:00' AND `upload_datetime` <= '".date("Y-m-d", strtotime("-$i day"))." 23:59:59'");
$result_uploads = mysqli_fetch_assoc($query_uploads);
}
请建议我,如何用更简单的方法解决这个问题,不要给 mysql 带来巨大的负载,谢谢:)