php - Select as total columns in a table based on the same ID -


i working on website (php , mysql) - involves management cpanel (database) of phone products.

i have issues on page, want display results:

my actual code is:

    <table data-order='[[0, "desc"]]' id="datatable-buttons" class="table table-hover m-0 table-bordered">       <thead>          <tr>             <th>entry id</th>             <th>producer</th>             <th>model</th>             <th>date</th>             <th>total sum</th>             <th>expenses</th>             <th>profit</th>           </tr>        </thead>  <tbody>   <?php     $result = mysql_query("select receptie.id                                  , receptie.marca_tel                                  , receptie.model                                  , receptie.data_primire                                  , articole_service.pret_sol                                  , articole_service.pret_achizitie                                  , articole_service.pret_sol - articole_service.pret_achizitie profit                         receptie                          inner join articole_service on receptie.id = articole_service.id_receptie                          order receptie.id desc");     while ($row = mysql_fetch_array($result))         {?>           <tr>             <td><?php echo $row['id']; ?></td>             <td><?php echo $row['marca_tel']; ?></td>             <td><?php echo $row['model']; ?></td>             <td><?php echo $row['data_primire']; ?></td>             <td><?php echo $row['pret_sol']; ?></td>             <td><?php echo $row['pret_achizitie']; ?></td>             <td><?php echo $row['profit']; ?></td>         </tr>   <?php } ?>  </tbody> </table> 

result now:

entry id        producer        model           date            total sum       expenses    profit  **21**              apple       galaxy s4           2016-09-01      150             122         28 **21**              apple       galaxy s4           2016-09-01      145             15          130 **20**              apple       iphone 4s           2016-09-06      145             12          133 **20**              apple       iphone 4s           2016-09-06      180             150         30 **20**              apple       iphone 4s           2016-09-06      150             1           149 

desired result: (to display total of columns based on same entry id , not duplicate rows)

entry id        producer        model           date            total sum       expenses    profit  21              apple       galaxy s4           2016-09-01      150+145         122+15      28+130 20              apple       iphone 4s           2016-09-06      145+180+150     12+150+1    133+30+149 

you should use aggregation function , group

  "select receptie.id     , receptie.marca_tel     , receptie.model     , receptie.data_primire     , sum(articole_service.pret_sol=     , sum(articole_service.pret_achizitie)     , sum(articole_service.pret_sol ) - sum(articole_service.pret_achizitie) profit     receptie      inner join articole_service on receptie.id = articole_service.id_receptie      group receptie.id     , receptie.marca_tel     , receptie.model     , receptie.data_primire     order receptie.id desc" 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -