【问题标题】:Codeigniter & PostgreSQL: Display posts by entry date or latest comment date, depending on which is newerCodeigniter 和 PostgreSQL:按条目日期或最新评论日期显示帖子,具体取决于哪个较新
【发布时间】:2011-03-15 15:34:06
【问题描述】:

我正在使用 CI,并设置了一个包含两个表的 PostgeSQL:Posts 和 Comments,它们都有 2 列:Id 和 Date。 Comments 还有一个 Parentid 列,以将其与与之相关的 Post 匹配。

现在我想显示按最新活动排序的帖子,即根据发布日期显示它们,或者如果有与帖子相关的评论,则按最新的评论日期显示。

以下设置...

帖子:

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 的活动记录实现这一点,还是我需要构建自定义查询,甚至进一步手动排序?

【问题讨论】:

    标签: database postgresql sorting codeigniter activerecord


    【解决方案1】:

    Active Record 已排序。你可以这样使用它...

    $this->db->order_by('date', 'asc');

    在这里查看...

    http://codeigniter.com/user_guide/database/active_record.html

    至于通过 cmets 或帖子进行的最新活动,您必须创建一个新的列别名,以获取 cmets 或帖子的最新值。

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多