sql结果总和

时间:2015-09-06 05:10:16

标签: php mysql sql

我从数据库表中提取所有内容并在php中显示特定数据。

到目前为止,我的所有代码都正常工作,我能够在php中显示所有内容,但我正在尝试制作另一个php文件,仅显示特定数据。例如,我希望它能够找到movieview表中的所有内容,并且只显示playcount等于或大于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'];

现在我希望echo $sum;将所有匹配c11的结果中的playcount的所有值加上等于或大于1的结果。我没有尝试过SQL的原因sum是因为下面我显示了更多列,我认为这是select *

的原因

编辑:如果我执行以下操作:

 $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;

它会显示播放次数为1或更多的所有项目的时间,这是好的。现在我只是希望总共得到所有那些$run值的总和并回应它。我还有额外的表在php文件中调用,只是一个注释,如mid,idb和c00。

现在,如果我这样做:      $ db_handle = mysql_connect($ server,$ username,$ password);      $ 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'];

我能够回应总数,是的!但是我无法调用上面列出的其他列。

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你可以这样做:

    LinearLayoutManager mLayoutManager;
recycle = (RecyclerView) findViewById(R.id.recycle);
        mLayoutManager = new LinearLayoutManager(this);
        recycle.setLayoutManager(mLayoutManager);

recycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
            boolean enable = false;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if(recycle != null && mLayoutManager.getChildCount() > 0){
                    boolean firstItemVisible = mLayoutManager.findFirstVisibleItemPosition() == 0;
                    boolean topOfFirstItemVisible = mLayoutManager.getChildAt(0).getTop() == 0;
                    Log.v("this",firstItemVisible + "  "+topOfFirstItemVisible);
                    //boolean topOfFirstItemVisible = recycle.getChildAt(0).getTop() == 0;
                    enable = firstItemVisible && topOfFirstItemVisible;
                }
                mSwipeRefreshLayout.setEnabled(enable);

            }
        });
    }

答案 1 :(得分:1)

如果你只是继续添加总运行它应该工作,如果我已正确理解

$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;

答案 2 :(得分:0)

我能够通过创建另一个SQL查询来实现。不确定它是多么合适但是有效。唯一缺少的是每个结果只计算1次,所以如果一个2或更多的游戏数量只被视为1.将在以后工作,除非你可以回复一个修复:D

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

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