---针对SQL Server 2008
Create Table Testing(TestID Integer Primary Key,Price Float);
Insert Into Testing Values (1,12.00);
Insert Into Testing Values (2,15.00);
Insert Into Testing Values (3,20.00);
Insert Into Testing Values (4,13.00);
Insert Into Testing Values (5,11.00);
Insert Into Testing Values (6,12.00);
Insert Into Testing Values (7,17.00);
Insert Into Testing Values (8,18.00);
Insert Into Testing Values (9,19.00);
Insert Into Testing Values (10,22.00);
Create Function dbo.GetMinID(@MaxId Integer,@NoOfRows Integer) Returns Integer
as
Begin
Declare @MinMax Integer
IF @NoOfRows > 1
Set @MinMax=dbo.GetMinID((Select MAX(TestID) From Testing where TestID <@MaxId),@NoOfRows-1)
else
Set @MinMax=(Select MAX(TestID) From Testing where TestID < @MaxId);
Return @MinMax;
End
Select R.TestID,dbo.GetMinID(R.TestID,8) as MinMax From (Select MAX(TestID) TestID
From Testing) R
你想这个SQL查询或你需要在你用PHP来做到这一点已经得到结果并将其保存为字符串?通过查询获得它会更好,我想这会需要更少的数据管理。 – Martin
只有SQL,也许PHP以后,现在我正在努力与SQL查询 –