sum mysql table column data where id is the same in php SELECT statement

时间:2016-10-20 20:10:05

标签: php mysql select

I need to display an html table with data from two different tables, invoices and lineitems where invoices.id=lineitems.invoiceid. However, each row in invoices can have several lineitem rows that connect to it as invoices is the customers' order and lineitems is the table containing all the products for each order. I need to sum up the values from lineitems.quantity for every row where the invoiceid is the same so my html table can display the total quantity for the order. Is there a way to do that within my select statement for my html table?

Right now, when I display my table, I get the same invoice order listed repeatedly for each lineitem row associated with it (where the only html table column data that's different per row is the quantity).

MySQL SELECT statement:

$sql = "SELECT invoices.id, invoices.orderdate, invoices.stagestatus, FORMAT(TRIM(LEADING '$' FROM invoices.totalprice), 2) AS totalprice, clients.company, lineitems.invoiceid, FORMAT((lineitems.width * lineitems.height) /144, 2 ) AS sqft, lineitems.quantity AS qty, FORMAT((invoices.totalprice / ((lineitems.width * lineitems.height) /144)), 2) as avgsqftrevenue, FORMAT((TRIM(LEADING '$' FROM invoices.totalprice) / lineitems.quantity), 2) AS avgunitrevenue
FROM clients
INNER JOIN invoices ON clients.id = invoices.clientid
INNER JOIN lineitems ON invoices.id = lineitems.invoiceid
WHERE invoices.orderdate BETWEEN '".$revenuefrom."' AND '".$revenueto."' AND invoices.stagestatus IN (". implode(',', array_map(function($item) {return '"' . $item . '"'; }, $revenue_check)) .")
ORDER BY invoices.id DESC";


//Display daterange and table.
echo 'Displaying results for: '.$revenuefrom.' to '.$revenueto.'. '.'<BR><BR><BR>';

$result = $conn->query($sql);



    echo "<table id='revenueReportA' align='center' class='report_DT'>
    <tr>

    <th>Customer</th>
    <th>Stage Status</th>
    <th>SG</th>
    <th>Revenue</th>
    <th>SQ FT</th>
    <th>AVG Revenue Per SQ FT</th>
    <th>Number of Units</th>
    <th>AVG Revenue Per Unit</th>
    </tr>";


 if ($result = $conn->query($sql)) {

  // fetch associative array 
 while ($row = $result->fetch_assoc()) {


   echo "<tr>";
   echo "<td>" . $row['company'] . "</td>";
   echo "<td>" . $row['stagestatus'] . "</td>";
   echo "<td>" . $row['id'] . "</td>";
   echo "<td>" ."$". $row['totalprice'] . "</td>";
   echo "<td>" . $row['sqft'] ."&nbsp;&nbsp;". "ft<sup>2</sup>". "</td>";
   echo "<td>" ."$". $row['avgsqftrevenue'] . "</td>";
   echo "<td>" . $row['qty'] . "</td>";
   echo "<td>" ."$". $row['avgunitrevenue'] . "</td>";
   echo "</tr>";
   } 

   echo "</table>";

NOTE: Due to the pre-existing code design (I inherited...), setting up a trigger is not an option and trying to run the calculation and insert that value into another column in invoices is seeming impossible from the application side (again due to the way the previous programmer designed the update/insert for invoices and lineitems). If anyone is curious about that code, please refer to the following link where I asked how complete this same task via the application side:

get sum() value from user input going into one table and store in another table using php

Any suggestions are greatly appreciated as I have tried several different methods for achieving the desired outcome.

Thank you!

0 个答案:

没有答案