下面是在Delphi中使用时的部分代码:
var
SqlStr: AnsiString;
SelfLevel: integer;
begin
SqlStr := 'select Level from ' + TableName + ' where ID=''' + ParentID + '''';
DataSet.Close;
DataSet.CommandText := SqlStr;
DataSet.Open;
SelfLevel := StrToInt(trim(DataSet.Fields[0].AsString));
SqlStr := 'select * from ' + TableName + ' where Level between ''' +
IntToStr(SelfLevel + 1) + ''' and ''' + IntToStr(SelfLevel + LevelNum) + ''' order by Level';
DataSet.Close;
DataSet.CommandText := SqlStr;
DataSet.Open;
end;
如果有这样一条SQL语句,并且你能提供正确写法,将立即加分!
在线等待!!
4 个解决方案
#1
请给出表结构、查询要求、示例数据。
#2
改一下where语句就可以了,直接将条件中的SelfLevel用上一SQL代替:
...
where Level>=(select Level from TableName where ID=ParentID)+1
and Level<=(select Level from TableName where ID=ParentID)+LevelNum order by Level
...
where Level>=(select Level from TableName where ID=ParentID)+1
and Level<=(select Level from TableName where ID=ParentID)+LevelNum order by Level
#3
表结构如下:
Trees(
ID char(10);
ParentID char(10);
Level char(10);
Family varchar(1000);
)
Trees(
ID char(10);
ParentID char(10);
Level char(10);
Family varchar(1000);
)
#4
Level 是字符串型!
#1
请给出表结构、查询要求、示例数据。
#2
改一下where语句就可以了,直接将条件中的SelfLevel用上一SQL代替:
...
where Level>=(select Level from TableName where ID=ParentID)+1
and Level<=(select Level from TableName where ID=ParentID)+LevelNum order by Level
...
where Level>=(select Level from TableName where ID=ParentID)+1
and Level<=(select Level from TableName where ID=ParentID)+LevelNum order by Level
#3
表结构如下:
Trees(
ID char(10);
ParentID char(10);
Level char(10);
Family varchar(1000);
)
Trees(
ID char(10);
ParentID char(10);
Level char(10);
Family varchar(1000);
)
#4
Level 是字符串型!