Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

php - How to get the sum of specific rows

i am developing a blood bank management system. Now i want to display that how many times each user donated blood, i mean i want to get the sum of each user row. For example if a donor(who's passport/IC is AF3444547)has 3 row in the database so i need show the total row from the database.If next time i put another record of AF3444547 then i need to get the sum 4. The image shown in below of my databse. Thanks in advance blood donation record

Here is my code

 <?php
                    $passport_IC='passport_IC';
                      $q="SELECT passport_IC, count(*) FROM donate GROUP BY passport_IC";
                      $query=$db->prepare($q);

                      $query->execute(array($passport_IC));
                      $people = $query->fetchAll(PDO::FETCH_OBJ);


                    ?>
     <table class="table table-bordered">
      <tr>
        <th><center> passport </center></th>
        <th><center> quantity</center></th>




      </tr>
      <?php foreach($people as $donors): ?>
        <tr>
           <td><center><b><font color="white"><?= $donors->passport_IC; ?></font></b></center></td>




        </tr>
      <?php endforeach; ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is not sum() what you need...

count(*) is the answer.

The COUNT (*) function returns the number of rows that satisfy the WHERE clause of a SELECT statement.

https://www.ibm.com/support/knowledgecenter/en/SSGU8G_12.1.0/com.ibm.sqls.doc/ids_sqs_1580.htm

 $q=$db->query("SELECT COUNT(*) from donate WHERE passport_ic=:passport_ic" );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...