Home > Backend Development > PHP Tutorial > 在数据库中使用sum()计算有错误?

在数据库中使用sum()计算有错误?

WBOY
Release: 2016-06-23 14:17:31
Original
1315 people have browsed it

我设定了两个表,表一为项目表tb_xm主要有项目批次xm_pcid和项目名称xm_name,表二为项目打分表主要有项目名称xmpf_xm、打分评委、和项目评分xmpf_small_zf。我现在想计算各评委所给出的总分,可是利用下列语句后得到的结果不对,例如项目名为a的项目,专家给分即xmpf_small_zf列值分别为0、1、1,可是得到的结果却为8。分值普遍要高6分,真是不知道哪里错了。劳大家帮我看看!

$xmpc_id='4';/******************************************/$query1=mssql_query("select * from tb_xm where xm_pcid='$xmpc_id'");		while($myrow1=mssql_fetch_assoc($query1)){		echo $myrow1[xm_name];		$xm_name = $myrow1['xm_name'];		$query2=mssql_query("select sum(xmpf_small_zf) as zf from tb_xmpf where xmpf_xm='$myrow1[xm_name]'");					//echo $query2;		echo "总分:";		while($myrow2=mssql_fetch_assoc($query2)){		 echo $myrow2[zf];		 $query3=mssql_query("update tb_xm set xm_zf='$myrow2[zf]' where xm_name='$myrow1[xm_name]'");		 }
Copy after login


回复讨论(解决方案)

条件不充分
select * from tb_xm where xm_pcid='$xmpc_id'
按批次取得项目名称,可知批次和项目名称并非一一对应的
可知一个项目名称可分属于不同的批次

select sum(xmpf_small_zf) as zf from tb_xmpf where xmpf_xm='$myrow1[xm_name]'
按项目名称取得得分
但是没有考虑到批次因素

mysql_fetch_assoc

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template