简体   繁体   English

sql结果之和

[英]sql sum of results

I am pulling everything from a database table and displaying specific data in php. 我正在从数据库表中提取所有内容,并在php中显示特定数据。

So far all my code is working, I'm able to display in php everything but I'm trying to make another php file to only display specific data. 到目前为止,我所有的代码都可以正常工作,我可以在php中显示所有内容,但是我试图制作另一个php文件以仅显示特定数据。 For example I want it to find everything in the movieview table and only display all row data that has a playcount of equal or greater than 1 and to display it by title ascending which is 例如,我希望它查找movieview表中的所有内容,并且仅显示playcount movieview等于或大于1的所有行数据,并按标题升序显示,即

 $SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc";
 $result = mysql_query($SQL);

 while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $db_field['c11'];
 $row = mysql_fetch_assoc($run); 
 $sum = $row['value_sum'];

Now I want to echo $sum; 现在我要echo $sum; to add up all values of c11 from all the results that matched playcount of equal or greater then 1. The reason I haven't tried the SQL sum is because below I am displaying more columns which i believe is the reason for select * 从与等于或大于1的playcount匹配的所有结果中求出c11所有值。我未尝试使用SQL sum的原因是因为我在下面显示了更多列,我相信这是select *的原因

edit: if i do the following: 编辑:如果我执行以下操作:

 $db_handle = mysql_connect($server, $username, $password);
 $db_found = mysql_select_db($database, $db_handle);

 if ($db_found) {

 $SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc";
 $result = mysql_query($SQL);

 while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $db_field['c11'];

 echo $run;

it does display the times of all the items with a playcount of 1 or more which is good. 它确实显示播放计数为1或更大的所有项目的时间,这很好。 now im just just wish to make a total of all those $run values in 1 sum and echo that. 现在,我只是希望将所有这些$run值总计为1并进行回显。 i also have extra tables that are called in the php file, just a note, like mid, idb, and c00. 我也有在php文件中调用的额外表,只是一个注释,例如mid,idb和c00。

now, if i do : $db_handle = mysql_connect($server, $username, $password); 现在,如果我这样做:$ db_handle = mysql_connect($ server,$ username,$ password); $db_found = mysql_select_db($database, $db_handle); $ db_found = mysql_select_db($ database,$ db_handle);

 if ($db_found) {

 $SQL = "select SUM(c11) AS totalrun from movieview WHERE playCount>=1 Order By c00 Asc ";
 $result = mysql_query($SQL);

 while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $db_field['totalrun'];

i am able to echo the totals, yey!, but i cant call on the other columns listed above. 我能够回应总数,是的!但是我无法调用上面列出的其他列。

If i understood well your question, you can do this: 如果我很了解您的问题,则可以执行以下操作:

$counter=0;

while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $db_field['c11'];
 $row = mysql_fetch_assoc($run); 
 $sum = $row['value_sum'];
 $counter .= $row['c11'];
}



echo $counter;

If you just keep on adding the total run it should work , If I have understood it correctly 如果您继续添加总运行次数,则应该可以正常运行,如果我已正确理解的话

$db_handle = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

 $SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc"; 
 $result = mysql_query($SQL);
 $run=0;
 while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $run + $db_field['c11'];
 }
echo $run;

i was able to do it by creating another sql query. 我能够通过创建另一个sql查询来做到这一点。 not sure how proper it is but it works. 不知道它是否合适,但是它可以工作。 the only thing missing is that each result is only counted 1x so if a playcount of 2 or more is only seen as 1. will work on that later unless you can reply with a fix :D 唯一缺少的是每个结果仅计为1倍,因此,如果播放次数等于或大于2的播放次数仅被视为1,除非您可以使用fix:D进行回复,否则以后将继续处理该结果。

 while ( $db_field = mysql_fetch_array($result) ) {

 $run = $db_field['SUM(c11)'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM