我已經看過了幾個不同的答案,這個問題似乎並不能得到查詢才能正常工作。Mysql的運行總計關閉一個表
這裏是我的表有列用戶,weekNo,salesTotalYTD。
我目前由周拉動這些出來,將它們分組,像這樣:
+------+--------+---------------+
| user | weekNo | salesTotalYTD |
+------+--------+---------------+
|Jared | 1 | 200 |
+------+--------+---------------+
| Jim | 1 | 50 |
+------+--------+---------------+
|Jared | 2 | 30 |
+------+--------+---------------+
| Jim | 2 | 100 |
+------+--------+---------------+
我所試圖做的,但不能完成如下:
+------+--------+---------------+
| user | weekNo | salesTotalYTD |
+------+--------+---------------+
|Jared | 1 | 200 |
+------+--------+---------------+
| Jim | 1 | 50 |
+------+--------+---------------+
|Jared | 2 | 230 |
+------+--------+---------------+
| Jim | 2 | 150 |
+------+--------+---------------+
這是查詢我已經工作了第一遍但之後每次傳球是錯誤的:
SET @runtot:=0
SELECT
salesTotalYTD,
user,
(@runtot := @runtot + salesTotalYTD) AS rt
FROM weeksAndSalesmantbl
GROUP BY user, weekNo
ORDER BY (CASE WHEN weekNo = 52 THEN 0 ELSE 1 END) ASC, weekNo, user ASC
更新
添但返回錯誤的更新代碼禮貌:
$assignments = "
SELECT
t1.user,
t1.weekNo,
(SELECT SUM(t2.salesTotalYTD) FROM weeksAndSalesmantbl t2
WHERE t2.user = t1.user AND t2.weekNo <= t1.weekNo) AS salesTotalYTD
FROM weeksAndSalesmantbl t1
ORDER BY
t1.weekNo,
t1.user";
$salesTotalSalesManCumulative = [];
$assignmentsqry = mysqli_query($db,$assignments);
if (!$assignmentsqry) {
printf("Error: %s\n", mysqli_error($db));
exit();
}
while ($row = mysqli_fetch_array($assignmentsqry)) {
$float = floatval($row['salesTotalYTD']);
$float = round($float,2);
array_push($salesTotalSalesManCumulative,$float);
}
你真的有'SELECT'出現兩次這樣呢? –
不,這是一個小姐副本,我糾正它。 –
我已提高您的問題。我們需要一個PHP人來看待這個問題並提出意見。我認爲你使用會話變量的原始方法也很好。 –