开发者

SQL - Select Fields of the Max ID

The last InvoiceID, and corresponding fields needs to be selected. The entire sql contains several inner joins.

SELECT max(InvoiceID), 
       InvoiceEndDate 
  FROM Invoices 
 WHERE TransactionOrderItemID = '000831'

Right now, I am getting the InvoiceID, and have to fetch the InvoiceEnd开发者_运维技巧Date again.

Is there an efficient way of doing this?


SELECT InvoiceID, InvoiceEndDate 
FROM Invoices 
WHERE TransactionOrderItemID='000831'
ORDER BY InvoiceID DESC
LIMIT 1


SELECT InvoiceID, InvoiceEndDate 
FROM Invoices INV
WHERE TransactionOrderItemID='000831'
  AND INV.InvoiceID = (SELECT MAX(SUB.InvoiceID)
    FROM Invoices SUB WHERE SUB.TransactionOrderItemID='000831');


Take a look at Including an Aggregated Column's Related Values which has several ways to accomplish this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜