例如有如下两个表
表topic:
id title
1 你好
2 我不好表reply:
id topic floor title addTime
1 1 null 回复:你好 <日期时间>
2 2 null 回复:我不好 <日期时间>
3 1 null 回复:你好 <日期时间>
4 2 null 回复:我不好 <日期时间>我想执行如下操作
select id from topic
然后循环取到的
select floor from reply where topic=<表topic的id> order by addTime desc
然后再根据时间来更新floor, 从1开始
rs("floor")=1
rs.movenext()
rs("floor")=2
然后再继续下一条topic的记录最后则变成
表reply:
id topic floor title addTime
1 1 1 回复:你好 <日期时间>
2 2 1 回复:我不好 <日期时间>
3 1 2 回复:你好 <日期时间>
4 2 2 回复:我不好 <日期时间>由于数据量非常大, 通过程序来嵌套循环来更新速度非常慢,
请问能够通过SQL来实现吗? 不好意思, 存储过程还不是很懂...
表topic:
id title
1 你好
2 我不好表reply:
id topic floor title addTime
1 1 null 回复:你好 <日期时间>
2 2 null 回复:我不好 <日期时间>
3 1 null 回复:你好 <日期时间>
4 2 null 回复:我不好 <日期时间>我想执行如下操作
select id from topic
然后循环取到的
select floor from reply where topic=<表topic的id> order by addTime desc
然后再根据时间来更新floor, 从1开始
rs("floor")=1
rs.movenext()
rs("floor")=2
然后再继续下一条topic的记录最后则变成
表reply:
id topic floor title addTime
1 1 1 回复:你好 <日期时间>
2 2 1 回复:我不好 <日期时间>
3 1 2 回复:你好 <日期时间>
4 2 2 回复:我不好 <日期时间>由于数据量非常大, 通过程序来嵌套循环来更新速度非常慢,
请问能够通过SQL来实现吗? 不好意思, 存储过程还不是很懂...
http://topic.csdn.net/u/20080324/10/82f22176-5d58-4683-8cab-8732b9b33d69.html
------------------------------对这句话不是很明白,是说按时间从大到小或从小到大,然后根据topic里的ID循环赋值?
假设现在有一个 帖子 表, 里面有100篇帖子,
有另一个 回复 表, 里面有10000篇回复, 回复有个字段是对应的帖子
现在我就是想, 回复表 里面的每个记录, 根据发表时间 来生成一个 楼层号
楼层号从1开始, 越后面发表的楼层号就越高, 步长为1
回复表里面会有很多个帖子的回复, 所以回复的楼层号并不是惟一的,
例如一个回复的楼层号为1, 他对应第1篇帖子,
但另一个回复的楼层号也为1, 他是第2篇帖子的第1篇回复可以这样理解, 假设如下表id, fromTopic, addTime, floor
1 1 1
2 4 2
3 2 3
4 2 4
5 1 5先按addTime取fromTopic=1的记录, 再根据发布时间顺序生成楼层ID,并更新到floor
然后再取fromTopic=2的记录...
fromTopic=1,2,3,4 这个数字必须是topic表中有的,
也就是说先查询topic表, 帖子的id是惟一的并开始循环
再根据topic的id查询reply表 reply.fromTopic=topic.id
再更新楼层号
然后再继续下一个topic的记录
create table tb(id int,topic int,[floor] int, title varchar(20), addTime varchar(20))
insert into tb values(1, 1, null , '回复:你好' , '<日期时间>')
insert into tb values(2, 2, null , '回复:我不好', '<日期时间>')
insert into tb values(3, 1, null , '回复:你好' , '<日期时间>')
insert into tb values(4, 2, null , '回复:我不好', '<日期时间>')
goselect id , topic , [floor] = (select count(*) from tb where topic = t.topic and id < t.id) + 1 , title , addtime from tb tdrop table tb/*
id topic floor title addtime
----------- ----------- ----------- -------------------- --------------------
1 1 1 回复:你好 <日期时间>
2 2 1 回复:我不好 <日期时间>
3 1 2 回复:你好 <日期时间>
4 2 2 回复:我不好 <日期时间>(所影响的行数为 4 行)
*/
(
select count(*)
from ddBbsReply
where ddAttachTopic = ddReply.ddAttachTopic
and ddAddTime < ddReply.ddAddTime
) + 1
from ddBbsReply ddReply
根本就不需要用到Topic这个表, 这样既节省时间, 也免去了很多不必要的查询
是我把问题想得太复杂了... 其实这个用法我早就会了.......为什么我没有想到呢? 这就是差距..... 55555555555我本来是想这样用程序来实现, 或者使用存储过程, 反正就是想的很复杂就是了, 没有找到这个投机取巧的方法...
今天早上已经更新过一次了, 就是用的下面的程序来实现的, 花了个把小时才执行完
刚刚团然发现自己粗心把 order by 写反了... 就想到看能不能直接用SQL来解决... 55555555555
var rs=createRecordset()
var rs2=createRecordset()
rs.open("select * from ddBbsTopic")
for(var i=0; i<rs.recordcount; i++){
rs2.open("select * from ddBbsReply where ddAttachTopic='"+rs("ddTopicID")+"' order by ddAddTime")
for(var o=0; o<rs2.recordcount; o++){
rs2("ddFloor")=(o+1)
rs2.moveNext()
}
rs2.close
}
recycle(rs2)
recycle(rs)谢谢楼上的各位达人...
update xxx set xxx=(select xxx from xxx)
update xxx set xxx='xxx'
貌似都不需要from子句, 可是上面那条sql没有from却又不能执行
from子句在update语句中到底是有什么作用呢?