请问能帮我把这条sql语句简化一下吗?(asp)

来源:百度知道 编辑:UC知道 时间:2024/05/26 17:46:09
数据库是一个目录树结构"Class表:"
id CateID(上级ID号)
1 0
2 0
3 0
4 1
5 1
6 1
7 4
8 4
......

Content 表:
id classid(class表的ID)

现在要查找content表中所有在class表指定ID下的信息
如:id=4,查找content表中classid=7 and 8 and 4
id=1,查找content表中classid=4 and 5 and 6 and 7 and 8 and 1

下面这条就能实现,可是也太.......
能不能简化一下?

sql="select top "&num&" * from Content where OnLocked=no and (ClassID="&id&" or ClassID in(select ID from Class where OnLocked=no and CateID="&id&" or CateID in(select ID from Class where OnLocked=no and CateID="&id&" or CateID in(select ID from Class where OnLocked=no and CateID="&id&" or CateID in(select ID from Class where OnLocked=no and CateID="&id&" or CateID in(select ID from Class where OnLocked=no and CateID="&id&"

不太清楚你用的什么库
如果可以用储存过程的话,可以写个递归的过程求出那些classid。

如果不是这样那最好和应用层结合一下。把求classid的任务交给它们,如果是网站方面可以把Class表生成个JS数组。
这样好些,要不数据库受不了