dtpk04526211 2016-03-29 07:21
浏览 32

安排有最新评论的帖子,并显示最新评论

our application has a comment table which looks like this(rough schema)

id(unique, primary, autoincrement)
post_id(20 varchar)
comment (160 varchar)
user_id(20 varchar)
created_at(timestamp)

how can we arrange unique posts in descending order of the comment posted on them, example

post 1 (date posted 16/03/16)(latest comment 04:00 PM)
post 2 (date posted 29/03/16)(latest comment 03:00 PM)

we are unable to formulate this, since our code is returning first comment not the latest

$this->db->select('*');
$this->db->distinct('post_id');
$this->db->order_by('created_at','DESC');
$this->db->limit(1);
$this->db->group_by('post_id');
$this->db->limit(10); // 10 unique post
$this->db->where('user_id',$user);
$query = $this->db->get('comments');
return $quer->result();
  • 写回答

1条回答 默认 最新

  • duanliusong6395 2016-03-29 07:30
    关注

    Try the below query:

    $query_result = $this->db->query('SELECT * FROM (
    SELECT *
    FROM comments
    WHERE user_id='.(int)$user.'
    ORDER BY id DESC) as temp
    GROUP BY post_id
    LIMIT 0,10');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM