如果我理解正确,那么您将需要加入具有父帖子日期的表格。如果可用,则应执行以下操作:
DECLARE @X TABLE(ID INT NOT NULL IDENTITY PRIMARY KEY,parentID INT,datePosted DATE NOT NULL)INSERT INTO @X (parentID, datePosted) VALUES (NULL, '2010-01-01'), (NULL, '2010-01-02'), (1, '2010-01-03'), (1, '2010-01-04'), (1, '2010-01-05'), (2, '2010-01-06')SELECt Post.parentID, Post.datePostedFROM @X AS Post LEFT JOIN @X AS Parent ON Post.parentID = Parent.IDORDER BY -- Order by post date, or rather the parent's post date if one exists COALESCE(Parent.datePosted, Post.datePosted) -- Order by reply date Post.datePosted
这给出了以下输出:
parentID datePosted-------- ----------NULL 2010-01-022 2010-01-06NULL 2010-01-011 2010-01-031 2010-01-041 2010-01-05
请注意,如果回复可以依次包含回复,则此操作会中断;您需要更强大的功能。在MS SQL中,我会使用CTE,但对Sqlite不太熟悉。



