代码点火器和PostgreSQL:按输入日期或最新评论日期显示帖子,具体取决于哪个较新
我正在使用 CI 并设置了一个 PostgeSQL,其中包含两个表:Posts 和 Comments,它们都有 2 列:Id 和 Date。评论还有一个 Parentid 列,用于将其与相关帖子相匹配。
现在我想显示按最新活动排序的帖子,即根据帖子日期显示它们,或者如果有与帖子相关的评论,则按最新评论日期显示。
以下设置...
帖子:
Id | Date
=========
1 | 04
2 | 07
3 | 08
评论:
Id | Parentid | Date
====================
1 | 1 | 04
2 | 2 | 07
3 | 1 | 09
...应按 1-3-2 的顺序返回帖子:1 有最新活动(评论),然后是 3(帖子),然后是 2(评论)。
有什么方法可以使用 CodeIgniter 的活动记录来实现此目的,或者我是否需要构建自定义查询,甚至进一步手动对它们进行排序?
I'm using CI and have a PostgeSQL set up with two tables: Posts and Comments, which both have 2 columns: Id and Date. Comments also has a column Parentid to match it with the Post it is related to.
Now I'd like to display the Posts ordered by latest activity, i.e. show them according to the Post Date or if there is a Comment related to the Post, by the latest Comment Date.
The following set-up...
Posts:
Id | Date
=========
1 | 04
2 | 07
3 | 08
Comments:
Id | Parentid | Date
====================
1 | 1 | 04
2 | 2 | 07
3 | 1 | 09
... should return the Posts in order 1-3-2: 1 has the latest activity (comment), then 3 (post), then 2 (comment).
Is there any way I can achieve this with CodeIgniter's active record or do I need to build a custom query or even further, sort them by hand?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Active Record 有排序依据。您可以像这样使用它...
$this->db->order_by('date', 'asc');
在这里查看...
http://codeigniter.com/user_guide/database/active_record.html< /a>
至于通过评论或帖子进行的最新活动,您必须创建一个新的列别名,以获取评论或帖子的最新值。
Active Record has order by. You can use it like so...
$this->db->order_by('date', 'asc');
Check it out here...
http://codeigniter.com/user_guide/database/active_record.html
As for the latest activity via comments or posts, your gonna have to create a new column alias that takes the latest value of either comments or posts.