SQL -选择最大ID的字段

时间:2022-09-26 13:03:33

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

需要选择最后的InvoiceID和相应的字段。整个sql包含几个内部连接。

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

Right now, I am getting the InvoiceID, and have to fetch the InvoiceEndDate again.

现在,我正在获取InvoiceID,并且必须再次获取InvoiceEndDate。

Is there an efficient way of doing this?

有没有一种有效的方法来做这件事?

3 个解决方案

#1


4  

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

#2


0  

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

#3


0  

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

看看包含聚合列的相关值,有几种方法可以实现这一点

#1


4  

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

#2


0  

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

#3


0  

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

看看包含聚合列的相关值,有几种方法可以实现这一点