---針對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查詢 –