SQLite 条件 ORDER BY 中的 DESC
我需要选择按以下逻辑排序的记录,但是当 DESC 处于条件中时,SQLite 会引发错误。
ORDER BY
CASE
WHEN parentGUID IS NULL THEN datePosted DESC
ELSE datePosted
END
这是为了实现类似于 Facebook 的排序 =- 原始帖子(始终具有空的parentGUID)按日期降序排列,并回复按日期升序排列的原始帖子。
I need to select records ordered by the following logic, however SQLite raises an error when DESC is in the conditional.
ORDER BY
CASE
WHEN parentGUID IS NULL THEN datePosted DESC
ELSE datePosted
END
This is to achieve Facebook like ordering =- original posts (which always have null parentGUIDs) in order descending by date and replies to original posts ordered by date ascending.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您需要加入到包含父帖子日期的表中。如果可用,应该这样做:
这给出了这样的输出:
请注意,如果回复可以轮流回复,这将中断;你需要更强大的东西。在 MS SQL 中,我会使用 CTE,但我对 Sqlite 不太熟悉。
If I understand you right, you'll need to join to the table that has the date of the parent post. If that's available, something like this should do:
This gives this output:
Note that this will break if replies can have replies in turn; you'd need something more robust. In MS SQL, I'd use a CTE, but I'm not very familiar with Sqlite.
经过你的纠正,我想我现在明白了。这是一些示例数据
,我猜您想要这个输出:
在单个查询中订购这非常困难(而且可能很慢)
With your correction, I think I now understand. Here's some sample data
And I'm guessing you want this output:
That's quite hard (and probably slow) to order in a single query