Home MYSQLI Query sum column duplicates value on rows
Reply: 0

MYSQLI Query sum column duplicates value on rows

user1429
1#
user1429 Published in June 21, 2018, 1:02 am

Add all column value 'price' of all duplicate invoice numbers on ORDERS-TABLE with _flag = y

I can achieve this in PHP array sorting or js but ideally, want this in the actual SQL query if possible.

Table_ORDERS

_id    |    _invoice_num    |    _name    |    _price    |    _flag

0          123                     bob           200          y
1          123                     bob           300          y
2          555                     mike          100          ...
3          123                     bob           300          y
3          888                     dave          200          y

Php:

<?php
 // im only after the query as its jsonen_code to ajax --

 $sql = 'select * , select(sum(price)) from table_orders Where _flag ='y' ;

 if ($results=mysqli_query($con,$sql)){

     while ($row=mysqli_fetch_row($results)){

            array_push($thearray,$row);

            } 
  } 

  echo json_encode(array_values($thearray));

 ?>

Output:

 /* output expecting array length 2 rows

 0 , 123 , bob , 800 , y
 1 , 888 , dave, 200 , y

 */

added:

 <?php

   $the_type = 'the_flag';

   //removes dupes 
   $sql = "SELECT * FROM orders WHERE $the_type = '' GROUP BY _invoice";

  // need to sum the dupes now.

  ?>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330351 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO