gpt4 book ai didi

php - 如何根据特定条件计算总和

转载 作者:行者123 更新时间:2023-11-29 03:47:21 24 4
gpt4 key购买 nike

我想根据用户名计算训练时数的总和。但是当我使用函数 SUM() 时,它会显示数据库中所有训练时间的总和。谁能帮帮我?

<?php
include('session.php');
include_once('DB_connect.php');

$UID = $_SESSION['USERID'];
if(isset($_POST["USER_NAME"])){
$B_title = strtoupper($_POST["USER_NAME"]);
$sql = "SELECT
TS_ID, TS_DATE, TS_DDATE, TS_HOUR,
TRAINING_NAME,TRAINING_HOUR, USER_NAME,
SUM(B.TRAINING_HOUR) AS TOTAL

FROM
TRAINING_STATUS TS,
USER_INFO UI,
TRAINING B
WHERE
TS.TRAINING_ID = B.TRAINING_ID
AND
TS.USER_ID = UI.USER_ID
";

include('sql.php');

if(mysql_num_rows($retval) > 0){}
print '<table align="right" style="width:14%">
<tr style="background-color:1AB299">
<th>TOTAL TRAINING HOUR </th>
</tr>';

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
print "<tr style='background-color:BCF9EF'>
<th>{$row['TOTAL']}</th>
</tr>";
}
}
?>

最佳答案

正如James指出的那样!

将查询更改为:

$sql = "SELECT 
TS_ID, TS_DATE, TS_DDATE, TS_HOUR,
TRAINING_NAME,TRAINING_HOUR, USER_NAME,
SUM(B.TRAINING_HOUR) AS TOTAL

FROM
TRAINING_STATUS TS,
USER_INFO UI,
TRAINING B
WHERE
TS.TRAINING_ID = B.TRAINING_ID
AND
TS.USER_ID = UI.USER_ID
GROUP BY USER_NAME";

关于php - 如何根据特定条件计算总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58091391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com