简体   繁体   English

在php中从MySQL中的多行添加

[英]Addition in php from more than one row in mysql

Hello I am trying to make a little game, where I need to add some point given for how a drink looks and taste. 您好,我正在尝试做一个小游戏,我需要在此添加一些关于饮料外观和口味的要点。

There will be 7 people giving points, so in php i need to create a table for each date and only show 1 name for each of the players, but I need the points to be added for every row with the same name: 将有7个人给出分数,因此在php中,我需要为每个日期创建一个表,并且只为每个玩家显示1个名称,但是我需要为每个具有相同名称的行添加分数:

Here is what I have so far. 这是我到目前为止所拥有的。

  <?php     

    $pdo = Logbase::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);      
    $sql = 'SELECT DISTINCT start FROM grade';      
    $q = $pdo->query($sql);
    $q->setFetchMode(PDO::FETCH_ASSOC);?> 
<?php while ($r = $q->fetch()): ?>
 <table class="blueTable" align="center">

<thead>

<tr>
<th> <?php echo $r['start'] ?></th>
<th></th>
</tr>
</thead>
<?php endwhile; ?> 
<tbody>
<?php     

    $pdo = Logbase::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);      
    $sql = 'SELECT * FROM grade group by start, name';      
    $q = $pdo->query($sql);
    $q->setFetchMode(PDO::FETCH_ASSOC);

  ?> 
 <?php while ($r = $q->fetch()): ?>                 
<tr>
<td> <?php echo $r['name'] ?></td>
<td> <?php echo $r['drinks_fla'] + $r['drinks_view']  + $r['food'] + 
 $r['sam'] ?></td>
</tr>
<?php endwhile; ?> 
</tbody>
</table>

In my result I can see 1 name for each player, but only 1 rows has been added (the first), so I quess I need a loop or something in my mysql? 在我的结果中,我可以为每个玩家看到1个名称,但是仅添加了1行(第一行),所以我是否需要循环或mysql中的某些内容?

Dennis --------10 point 丹尼斯-------- 10分
Michael--------6 point 迈克尔-------- 6分

I want to see Dennis 24 point Michale 12 points 我想看丹尼斯24分米歇尔12分

id---start--- name-----drinks_fla---drinks_view id ---开始---名称----- drinks_fla --- drinks_view

1---31.07-2017---dennis---5---5 1 --- 31.07-2017 ---丹尼斯--- 5 --- 5
1---31.07-2017---dennis---4---3 1 --- 31.07-2017 ---丹尼斯--- 4 --- 3
1---31.07-2017---dennis---5---2 1 --- 31.07-2017 ---丹尼斯--- 5 --- 2
2---31.07-2017---Michale---5---1 2 --- 31.07-2017 ---米歇尔--- 5 --- 1
2---31.07-2017---Michael---2---4 2 --- 31.07-2017 ---迈克尔--- 2 --- 4

$sql = 'SELECT * FROM grade group by start, name'; 

You have grouped your Results from MySQL, so only one Row will be called for the Properties of the Group. 您已经将来自MySQL的结果进行了分组,因此对于组的属性将仅调用一行。

Try "sum()" to get sum of the selected Columns you want: 尝试“ sum()”以获取所需的选定列的总和:

$sql = 'SELECT name as name,sum(sum(drinks_fla),sum(drinks_view)) as sum FROM grade group by start, name';

MySQL will call all Rows, ordered by the first Property. MySQL将调用所有由第一个Property排序的行。 All Rows with the same first Property are sorted with the second one. 具有相同第一个属性的所有行均与第二个进行排序。

Example: 例:

id---start--- name-----drinks_fla---drinks_view

1---31.07-2017---dennis---5---5
1---31.07-2017---dennis---4---3
1---31.07-2017---dennis---5---2
2---31.07-2017---Michale---5---1
2---31.07-2017---Michael---2---4

Result:
name----sum
dennis----24
Michale----6
Michael----6

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

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