我有一个包含这样数据的表:
MainID, MainNumber, MainType
1 250 Hours
2 500 Hours
3 1000 Hours
4 5000 Miles
5 20000 Miles
当我为MainNumber和MainType设置值时,我需要为该MainType提取下一个较低的MainNumber。
这是我的代码:
SQL = "SELECT T2.MainID, Max([t2.MainNumber]) AS PreviousCycle"
SQL = SQL & " FROM tblMainType AS T1 LEFT JOIN tblMainType as T2 on T1.MainNumber > T2.MainNumber"
SQL = SQL & " WHERE T1.mainType = '" & mType & "' AND T1.MainNumber = " & MainNumber
SQL = SQL & " Group By T2.MainID;"
奇怪的是它可以工作几个小时而不是几英里?我的代码出了什么问题?
答案 0 :(得分:0)
要查找下一个最高的数字,我会按降序对字段进行排序并获取TOP 1
值。
Sql = "SELECT TOP 1 tblMainType.MainID, tblMainType.MainNumber " & _
"FROM tblMainType " & _
"WHERE (((tblMainType.MainNumber)< " & mType & ") AND ((tblMainType.MainType)='" & MainNumber & "')) " & _
"ORDER BY tblMainType.MainNumber DESC;"
答案 1 :(得分:0)
我希望这样的查询:
SELECT TOP 1 MainID, MainNumber
FROM tblMainType
WHERE MainType = @MainType AND
MainNumber >= @MainNumber
ORDER BY MainNumber DESC, MainId;
@MainType
和@MainNumber
是您输入的值。