根据相同的ID选择表中的总列数

时间:2016-09-12 19:10:36

标签: php mysql sql database

我正在开发一个网站(PHP和MySQL) - 它涉及某些手机产品的管理cpanel(数据库)。

我在页面上遇到了一些问题,我想在其中显示一些结果:

我的实际代码是:

    <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 as profit
                        FROM receptie 
                        inner join articole_service on receptie.id = articole_service.id_receptie 
                        order by 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>

现在结果:

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

期望的结果:(显示基于相同条目ID而不是重复行的列的总数)

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

3 个答案:

答案 0 :(得分:1)

select entry id,producer,model,date,sum(total sum),sum(expenses),sum(profit)
from table_name group by entry id;

答案 1 :(得分:0)

您应该使用聚合功能和分组

  "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) as profit
    FROM receptie 
    inner join articole_service on receptie.id = articole_service.id_receptie 
    group by receptie.id
    , receptie.marca_tel
    , receptie.model
    , receptie.data_primire
    order by receptie.id desc"

答案 2 :(得分:0)

您需要GROUP BY要分组的内容,然后将SUM用于添加的字段。然后,您可以单引号以使列名具有空格:

SELECT receptie.id AS 'Entry ID'
    , receptie.marca_tel AS 'Producer'
    , receptie.model AS 'Model'
    , SUM(receptie.data_primire) AS 'Date'
    , SUM(articole_service.pret_sol) AS 'Total sum'
    , SUM(articole_service.pret_achizitie) AS 'Expenses'
    , SUM(articole_service.pret_sol - articole_service.pret_achizitie) as 'Profit'
FROM receptie 
    inner join articole_service on receptie.id = articole_service.id_receptie 
GROUP BY (receptie.id, receptie.marca_tel, receptie.model)
order by receptie.id desc