如何在mysql数据库中的特定列中选择最后一个值?

时间:2015-12-18 10:48:06

标签: php mysql

我目前正在使用此查询从invoiceNo不同的表中获取所有值。

SELECT * FROM  `selected_items` WHERE invoiceNo IN ( SELECT DISTINCT ( invoiceNo) AS invoiceNo FROM selected_items ) GROUP BY invoiceNo;

这是我的表结构

enter image description here

我想从最后一行访问所有内容。我当前的查询给了我第一行。谢谢你:)

5 个答案:

答案 0 :(得分:0)

使用order by rownumGROUP 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

答案 1 :(得分:0)

试试这个,

SELECT * FROM selected_items WHERE invoiceNo IN ( SELECT DISTINCT ( invoiceNo) AS invoiceNo FROM selected_items ) GROUP BY invoiceNo ORDER BY custInfo DESC LIMIT 1;

答案 2 :(得分:0)

您应该使用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;

答案 3 :(得分:0)

是吗?:

SELECT DISTINCT invoiceNo, barcode, description, weight, rate, makingAmt, net_rate, itemTotal, vat 
FROM selected_items 
GROUP BY invoiceNo 
ORDER BY date DESC LIMIT 1;

要获取最后一行,应该有一些包含唯一值的字段。

答案 4 :(得分:0)

如果您想获得每个sum_total的最高invoiceNo,那么您可以执行以下操作:

SELECT invoiceNo, MAX(sum_total) AS sumTotal
FROM selected_items 
GROUP BY invoiceNo;