简体   繁体   English

Mysql和php的多列求和

[英]SUM of multiple columns whit Mysql and php

I have been trying to get a query to work whit a SUM of columns like (kill, death and assist for players. 我一直在尝试获取一个查询,以查询诸如(杀死,死亡和对玩家的协助)之类的列之和。

I have been trying whit the follow php mysql code: 我一直在尝试以下php mysql代码:

        <div class="row">
            <h3>ScoreBoard Game Info</h3>
        </div>
        <div class="row">

            <table class="">
              <thead>
                <tr>
                  <th>Player</th>
                  <th>Kills</th>
                  <th>Death</th>
                  <th>Assist</th>
                </tr>
              </thead>
              <tbody>

              <?php
               include 'database.php'; //DB connect

               $pdo = Database::connect();
               $sql = 'SELECT player, sum(kill) as kill, SUM(death) as death, SUM(assist) as assist FROM eventgame GROUP BY player ORDER by player';

               foreach ($pdo->query($sql) as $row) {
                        echo '<tr>';
                        echo '<td>'. $row['player'] . '</td>';
                        echo '<td>'. $row['kill'] . '</td>';
                        echo '<td>'. $row['death'] . '</td>';
                        echo '<td>'. $row['assist'] . '</td>';
                        echo '<td width=250>';
                        echo '</td>';
                        echo '</tr>';
               }
               Database::disconnect();
              ?>
              </tbody>
        </table>
    </div>
</div> <!-- /container -->

I'm not been able to get this to work, can someone pls point what I'm doing wrong? 我无法使它正常工作,有人可以指出我做错了什么吗? I'm trying this for some days now. 我现在已经尝试了几天。 If the problem is the query can someone point what do I need to change or I need another approach? 如果问题是查询,那么有人可以指出我需要更改什么还是需要其他方法? I'm using mysql 5.6. 我正在使用mysql 5.6。 AW what so i need its the TOTAL of each player. 哇,所以我需要每个球员的总和。

MySQL supports expressions. MySQL支持表达式。 You might try: 您可以尝试:

Select kill + death as  total, , , 

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

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