我需要每天运行这个脚本(cron)来更新 1 个月的反馈总量(至少这是我现在设计的)。这是我的代码。有人对我应该如何解决这个问题有更好的了解吗?也许改变我的方式或优化我的 updateMonthlyFeedback.php 脚本?
updateMonthlyFeedback.php
session_start();
include("db.php");
$sql="SELECT MAX(uid) as maxUID FROM users";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$maxUID = $row['maxUID'];
for($i=0;$i<$maxUID;$i++){
$sql="SELECT COUNT(*) as negativeCount FROM feedbacks WHERE date_created >= ( CURDATE() - INTERVAL 30 DAY ) AND type = -1 AND uid = '$i'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$negativeCount = $row['negativeCount'];
$sql="SELECT COUNT(*) as neutralCount FROM feedbacks WHERE date_created >= ( CURDATE() - INTERVAL 30 DAY ) AND type = 0 AND uid = '$i'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$neutralCount = $row['neutralCount'];
$sql="SELECT COUNT(*) as positiveCount FROM feedbacks WHERE date_created >= ( CURDATE() - INTERVAL 30 DAY ) AND type = 1 AND uid = '$i'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$positiveCount = $row['positiveCount'];
$sql = "UPDATE feedback_totals SET negativeCount = '$negativeCount', neutralCount = '$neutralCount', positiveCount = '$positiveCount' WHERE uid = '$i'";
$result=mysql_query($sql) or die(mysql_error());
}
MySQL 表
CREATE TABLE feedback_totals (
uid VARCHAR(40),
negativeCount int,
neutralCount int,
positiveCount int,
halfStarCount int,
oneStarCount int,
oneHalfStarCount int,
twoStarCount int,
twoHalfStarCount int,
threeStarCount int,
threeHalfStarCount int,
fourStarCount int,
fourHalfStarCount int,
fiveStarCount int,
PRIMARY KEY (uid)
#FOREIGN KEY (uid) REFERENCES users(uid) ON DELETE CASCADE
);
CREATE TABLE feedback_last_month (
uid VARCHAR(40),
negativeCount int,
neutralCount int,
positiveCount int,
halfStarCount int,
oneStarCount int,
oneHalfStarCount int,
twoStarCount int,
twoHalfStarCount int,
threeStarCount int,
threeHalfStarCount int,
fourStarCount int,
fourHalfStarCount int,
fiveStarCount int,
PRIMARY KEY (uid)
#FOREIGN KEY (uid) REFERENCES users(uid) ON DELETE CASCADE
);
CREATE TABLE feedback (
feedback_id INT NOT NULL AUTO_INCREMENT,
uid VARCHAR(40),INDEX (uid),
sender_id VARCHAR(40),
type int, #-1 = neg, 0 = neutral, 1 = positive
starCount VARCHAR(40),
description VARCHAR(80),
date_created timestamp DEFAULT CURRENT_TIMESTAMP,
fromType VARCHAR(40), # buyer or seller
fromUsername VARCHAR(40),
PRIMARY KEY (feedback_id)
#FOREIGN KEY (uid) REFERENCES users(uid) ON DELETE CASCADE
);