简体   繁体   中英

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:

        <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. AW what so i need its the TOTAL of each player.

MySQL supports expressions. You might try:

Select kill + death as  total, , , 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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