5

This is my php script that selects everything from invoiceNo where invoiceNo is distinct.

<?php

require 'init.php';
$query = 'SELECT * FROM  `selected_items` WHERE invoiceNo IN ( SELECT DISTINCT ( invoiceNo) AS invoiceNo FROM selected_items ) GROUP BY invoiceNo;';
$res = mysqli_query($con, $query);
$result = [];
while ($row = mysqli_fetch_array($res)) {
    array_push($result, [
        'custInfo'    => $row[0],
        'invoiceNo'   => $row[1],
        'barcode'     => $row[2],
        'description' => $row[3],
        'weight'      => $row[4],
        'rate'        => $row[5],
        'makingAmt'   => $row[6],
        'net_rate'    => $row[7],
        'itemTotal'   => $row[8],
        'vat'         => $row[9],
        'sum_total'   => $row[10],
        'bill_type'   => $row[11],
        'date'        => $row[12],
        'advance'     => $row[13],
        'balance'     => $row[14],
    ]);
}
echo json_encode(['result' => $result]);
mysqli_close($con);

enter image description here

Right now this script gives me the first value from sum_total i.e it gives me the first row from my database how can I get the last row.I am new to programming any suggestions or help is appreciated.Thanks :)

4
  • why don't you use sorting DESC? Commented Dec 18, 2015 at 6:08
  • @AjayMakwana can you give an example.Thanks :) Commented Dec 18, 2015 at 6:09
  • i think you want max sum_total from the unique invoice right ? do you want all invoice with that or only sort by desc sum_total Commented Dec 18, 2015 at 6:19
  • @rahul yes exactly.Can you help! Commented Dec 18, 2015 at 6:21

4 Answers 4

2
Select * From (
SELECT t.*, 
       @rownum := @rownum + 1 AS rank
  FROM selected_items t, 
       (SELECT @rownum := 0) r order by rank DESC
) si GROUP BY si.invoiceNo;

This query solved my problem

Sign up to request clarification or add additional context in comments.

Comments

0

Try this, i think this is you want, may it help

$query ="SELECT max( `sum_total` ) FROM `selected_items` GROUP BY invoiceNo;";

9 Comments

else you can modify it on your requirement or tell me what exactly you want
Rahul but also want all the values from that row.Can't I just get the last row for that particular invoice.Thanks :)
yeah sure just add the selected values by comma after max('sum_total'), example_field from .....
ya i tried that but it still gets the first row I have no idea why?
in your db there is no unique field to identify the last inserted record its same values for all the fields even the date except sum_total, can you make your date differ so we can try by fetching record by last updated date time
|
0

try like this :

$query ="SELECT * FROM  `selected_items` WHERE invoiceNo IN ( SELECT DISTINCT ( invoiceNo) AS invoiceNo FROM selected_items ) ORDER BY `sum_total` DESC";
$query ="SELECT max( `sum_total` ) FROM selected_items";

Where column_name can be vary.

3 Comments

This gives the same result I still get the first row
@AjayMakwana your second query will give only one record
you want all the record and the first record should with maximum total? Then try removing group by.
0

If you need to get only last record use limit.

$query ="SELECT * FROM  `selected_items` GROUP BY invoiceNo ORDER BY `sum_total` DESC limit 1;

If you need to get highest to lowest sum_total record try the below code,

$query ="SELECT * FROM `selected_items` where `sum_total` = (SELECT max( `sum_total` ) FROM `selected_items` GROUP BY invoiceNo) GROUP BY invoiceNo ORDER BY `sum_total` DESC;

6 Comments

Thanks Arun I tried both your solutions the first one still gives me the first row.And the 2nd one gives an error saying 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC LIMIT 0, 30' at line 1
I missed to add sum_total for order by in 2nd query. Now edited it. Ok. I need to know one thing clearly, Do u want all records with unique invoiceNo or Final record only?
The second one gives an error #1242 - Subquery returns more than 1 row
instead of ORDER BY sum_total DESC use ORDER BY GREATEST(sum_total) in 1st query and let me know.
I tried this SELECT * FROM selected_items GROUP BY invoiceNo ORDER BY GREATEST (sum_total) DESC limit 1; it gave an error #1582 - Incorrect parameter count in the call to native function 'GREATEST'
|

Your Answer

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