I am currently using this query to fetch all the values from a table where the invoiceNo is distinct.
SELECT * FROM `selected_items` WHERE invoiceNo IN ( SELECT DISTINCT ( invoiceNo) AS invoiceNo FROM selected_items ) GROUP BY invoiceNo;
This is my table structure
And I would like to access everything from the last row.My current query gives me the first row.Thank you :)
use order by rownum
or GROUP BY
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
试一试
SELECT * FROM selected_items WHERE invoiceNo IN ( SELECT DISTINCT ( invoiceNo) AS invoiceNo FROM selected_items ) GROUP BY invoiceNo ORDER BY custInfo DESC LIMIT 1;
您应该使用ORDER BY
列名,这样您将首先获得最后一个条目
SELECT * FROM `selected_items` WHERE invoiceNo IN ( SELECT DISTINCT ( invoiceNo) AS invoiceNo FROM selected_items ) GROUP BY invoiceNo ORDER BY invoiceNo DESC LIMIT 1;
Is it?:
SELECT DISTINCT invoiceNo, barcode, description, weight, rate, makingAmt, net_rate, itemTotal, vat
FROM selected_items
GROUP BY invoiceNo
ORDER BY date DESC LIMIT 1;
to get last row there should some fields that contains unique values.
If you want to get the highest sum_total
of each invoiceNo
then you can do something like this,
SELECT invoiceNo, MAX(sum_total) AS sumTotal
FROM selected_items
GROUP BY invoiceNo;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.