sql查询tbl评论

时间:2011-11-06 04:30:58

标签: sql self-join

Comment:tbl

CommentId PK  
CommentEntry  
CommentDate  
Category  
CommentCommentId  
BlogEntryId FK


Insert into Comment(CommentId, CommentEntry, CommentDate, Category, CommentCommentId, BlogEntryId)
Values("1","I love shoes","10-3-2011","1","" ,"13"),
("2","I love shoes too","10-4-2011","1","1" ,"13"),
("3","There nice shoes in the mall","2","","13");

问题:对于给定博客条目的第一条评论,仅针对该原始评论发表任何进一步的评论。应提示用户输入博客条目的唯一标识符。

ANS支持:

("1","I love shoes","10-3-2011","1","" ,"13"),
("2","I love shoes too","10-4-2011","1","1" ,"13")

请帮助sql

我想做一个自我加入接受[BlogEntryId]和产品第一评论'CommentCommentId =“”'以及对第一条评论的所有评论。

请帮助sql:)

2 个答案:

答案 0 :(得分:0)

我猜你想要这样的东西:

select * 
from comment c
join comment subc on c.commentcommentid = subc.commentid
where c.commentid = @inputID

这是一个自我加入。

我认为家庭作业问题的答案就是:

select *
from comment
where commentcommentid = @inputid

答案 1 :(得分:0)

只需在原始查询中添加一点:

SELECT * 
FROM Comment 
WHERE BlogEntryId = [?Blog Entry] 
AND (
    CommentId = (SELECT CommentId FROM Comment WHERE CommentDate = (SELECT min(CommentDate) from Comment))
    OR
    CommentCommentId = (SELECT CommentId FROM Comment WHERE CommentDate = (SELECT min(CommentDate) from Comment))
    )