我正在开发一个这样的系统(完全不同的主题),回复和评论与我们每天在StackOverflow上看到的系统相似。
我的问题是,我正在用一个存储的PROC加载问题,用另一个存储的PROC加载回复,现在我添加了评论系统。我是否需要为每个关于主题的回复逐一获取评论?
这意味着如果我将页面大小设置为20个回复,我将执行22个数据库操作,这比我想象的要多。
我不认为我需要为这个问题添加我的数据库图,但它仍然在这里:
Questions
-----------
QUESTION_ID
USER_ID
QUESTION_TEXT
DATE
REPLIES
-----------
REPLY_ID
QUESTION_ID
USER_ID
REPLY_TEXT
DATE
COMMENTS
------------
REPLY_ID (fk replies)
USER_ID
TEXT
DATE
发布于 2011-03-01 03:47:53
你应该立刻得到你所有的评论。
然后使用每个回复的过滤器从结果生成DataView
s,并绑定到该DataView
。您还可以使用linq to entities,并在每次绑定时过滤出新的集合。下面是一个基本的伪代码示例:
OnDataBinding
OnDataBinding
为具有相同回复ID的评论的结果集添加筛选器这应该是可行的,我已经为相似类型的数据结构实现了相同的场景。
发布于 2011-03-01 03:24:16
帕布克
对于您的初始问题,为什么不使用给定问题/回复的单个查询来获得所有结果?
select reply_text, user_id
from REPLIES
order by DATE asc
此外,正如您所指出的,除了细微的差异之外,问题和答案与帖子的属性几乎相同。
像下面这样的模型不是更有意义吗?问题和答案都是“帖子”,唯一的区别是答案将问题作为父问题,而问题没有父问题。
Create table post -- question/reply (
post_id number,
parent_post_id number, -- will be null if it is the question, will have the question id
-- if it is a reply to a question
post_text varchar2(4000),
user_id number,
post_date date);
-self referential foreign key
Alter table post
add constraint foreign key (parent_post_id) references post(post_id);
--对所有帖子的评论(问题/回复)。
create table comments(
comment_id number,
post_id number,
comment_txt varchar2(140),
comment_user_id number,
comment_date date
);
alter table comments add constraint fk_comments_post
foreign key (post_id) references post(post_id).
--对于给定的问题(帖子) id,您可以使用...获取所有回复和帖子。
select replies.*,
comments.*
from posts replies,
comments
where replies.parent_id = :Question_id --input
and comments.post_id = replies.post_id
您可能需要添加一个order by子句,以便根据点、updated_timestamp或任何其他所需属性获得结果。
https://stackoverflow.com/questions/5146366
复制相似问题