How to access a specific value from mysql database using php?

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

<?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 is giving me the first value from sum_total, that is, it is giving me the first row from my database, how can I get the last row. I am new to programming any suggestions or help. Thanks:)

+5
source share
4 answers
 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 request solved my problem

+2
source

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 may vary.

0
source

Try this, I think this is what you want, can it help

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

If you need to get only the last record usage limit.

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

If you need to get the highest smallest sum_total entry, try the code below,

 $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; 
0
source

Source: https://habr.com/ru/post/1238598/


All Articles