1

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.

  ?>
7
  • so just get the sum of duplicate entries? Commented May 17, 2018 at 2:43
  • no i need the complete rows still but just adding the duplicate values together and displaying the duplicate row once as the OUTPUT: Commented May 17, 2018 at 2:45
  • shouldn't group by do what you need? It will still display all other records but combine duplicates. Commented May 17, 2018 at 2:47
  • The above is an example simplified but that not how i am implementing the actual issue. essentially i want to display all rows but only one of a single duplicate and add all them duplicates together on its price column Commented May 17, 2018 at 2:51
  • group by ( removes dupes) then i want to sum the dupes but still display atleast 1 record of it then the rest as normal.. Basically every item has an invoice number so if you buy 2 phones they share the same invoice number because there on the same invoice but when i display them or search i dont want to show each row rather just show all invoice numbers and totals of cost as each row as long as the flag = 'y' Commented May 17, 2018 at 3:04

1 Answer 1

1

This should work:

SELECT *,
       SUM(_price) AS total
FROM   _orders
WHERE  _flag = 'y'
GROUP  BY _invoice_num  
Sign up to request clarification or add additional context in comments.

1 Comment

I was missing the comma after * symbol.. This works, but i will say it adds an extra column at end of the row results with as total. This does solve my problem just not how i expected it. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.