我的表有(SubId,QId,Question,AnswerOptions,Ans)。 SubId是外键,QId是主键。我需要选择最新插入的记录并将其显示在文本框中。
我已经尝试过这些查询
SELECT * FROM tblQuestions
SELECT SCOPE_IDENTITY ()
SELECT MAX(QId) FROM tblQuestions
SELECT TOP 1 QId
FROM tblQuestions
ORDER BY QId DESC
但是我得到了第一条记录,当我第一次插入时,即使我尝试MAX()
它显示值1,我尝试MIN()
它也显示1。如何获得最新价值?
注意:最新值是QId的最大值。
答案 0 :(得分:0)
由于Qld是varchar类型,因此应该起作用:
SQL Server:
SELECT TOP 1 *
FROM tblQuestions
ORDER BY Qld ASC
MySQL:
SELECT Qld FROM tblQuestions
ORDER BY Qld ASC
LIMIT 1
MS Access:
SELECT LAST(Qld)
FROM tblQuestions
答案 1 :(得分:0)
试试这个会起作用
;WITH x AS
(
SELECT
*, r = RANK() OVER (ORDER BY QId DESC)
FROM tblQuestions
)
SELECT *
FROM x
WHERE r = 1;
答案 2 :(得分:0)
WITH get AS
(
SELECT
*, row = ROW_NUMBER OVER (ORDER BY qid DESC)
FROM tbl
)
SELECT *
FROM get
WHERE row = 1;
答案 3 :(得分:-1)
SCOPE_IDENTITY()
从该表中获取Identity Property的表我们可以获得最后插入的记录ID,就像这样
SELECT SCOPE_IDENTITY()