2014-01-11 42 views
0

Hello guys我想获取where /和子句中列的最后一个id。获取where /和子句列中的最后一个id mysql/sql

这是我已经试过:

SELECT i.documentnumber, i.documentseq, i.transactiondate, v.vancode, i.itemcode, i.qty, i.amount, i.reason, i.posted, i.dateposted, i.unitprice 
FROM intrans AS i 
JOIN intrans_vancode AS v ON i.locationcode = v.vancode 
WHERE posted = 0 
ORDER BY v.id DESC 

但我发现了一组功能的错误/问题。

任何想法如何实现我想要的?谢谢你的帮助。

回答

1

如果你只是寻找具有最大ID的一个记录,为什么不使用一个变量:

Declare @MaxID int 
Set @MaxId = (Select Max(id) From intrans_vancode) 

Select 
    i.documentnumber, 
    i.documentseq, 
    i.transactiondate, 
    v.vancode, 
    i.itemcode, 
    i.qty, 
    i.amount, 
    i.reason, 
    i.posted, 
    i.dateposted, 
    i.unitprice 
FROM intrans AS i 
    Join intrans_vancode v On i.locationcode = v.vancode 
WHERE i.posted = 0 -- you didn't specify a prefix so I don't which table this column belongs to 
AND v.id = @MaxID