【问题标题】:SQL - Paging commentsSQL - 分页注释
【发布时间】:2015-05-19 07:29:11
【问题描述】:

我有下面的表格评论:

CREATE TABLE Comment
(
     CommentID int,
     Username nvarchar(50),
     Content nvarchar(255),
     ReplyID int,
     primary key (CommentID),
)

ALTER TABLE dbo.Comment
ADD FOREIGN KEY (CommentID) REFERENCES dbo.Comment (CommentID) ON DELETE NO ACTION ON UPDATE NO ACTION,

我想用数据查询详细产品的分页评论:

+----+----------+-------------+---------+
| ID | Username |   Content   | ReplyID |
+----+----------+-------------+---------+
|  1 | UserA    | hello       | null    |
|  2 | UserB    | hello       | null    |
|  3 | UserC    | Hi UserA    | 1       |
|  4 | UserD    | Hello World | null    |
|  5 | UserE    | Hi UserB    | 2       |
+----+----------+-------------+---------+

如何显示带有分页的 cmets,并且 displayPerPage = 2,例如:

UserA: Hello

       UserC: Hi UserA

UserB: Hello

       UserE: Hi UserB

>>More Comments<<

任何帮助将不胜感激。

解决方案 1: 使用 外连接

【问题讨论】:

  • 您使用的是哪个 RDBMS?
  • 我有使用[Outer Join]的解决方案,但我不知道如何查询分页cmets
  • 请将您的解决方案添加到问题中。
  • 微软 Sql 服务器 2008
  • 我知道如何做到这一点 UserA: Hello: UserC: Hi UserA 但不是你的......在不同的行中

标签: sql sql-server sql-server-2008 comments paging


【解决方案1】:

您可以使用类似于以下内容的查询来以正确的顺序获取评论:

select case when c.ReplyId is not null then '        ' else '' end
     + UserName + ': ' + c.content Line
  from Comment c
 order by IsNull(c.ReplyId, c.CommentId), c.commentId

【讨论】:

    猜你喜欢
    • 2015-10-17
    • 2016-11-30
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多