douhui5529 2014-03-25 07:01 采纳率: 100%
浏览 31
已采纳

将查询转换为codeigniter格式

Hello guy's i have this query

SELECT distinct blog.*,(select count(blog_id) from blog_comment where blog_comment.blog_id=blog.id) as comment FROM blog Left JOIN blog_comment ON blog.id = blog_comment.blog_id

I want this query in CI format.this query having subquery in his column so I m facing problem to convert this query in to CI format i don't to how to do this please help

  • 写回答

2条回答 默认 最新

  • dou448172583 2014-03-25 07:04
    关注

    You can do so

    $this->db->select('distinct blog.*,
    (select count(blog_id) from blog_comment where blog_comment.blog_id=blog.id) as comment ',FALSE);
    $this->db->from('blog')
    $this->db->join('blog_comment','blog.id = blog_comment.blog_id','LEFT');
    $this->db->get();
    

    You original query can be rewritten without using subquery

    SELECT DISTINCT 
      b.*,
      COUNT(bc.blog_id) `comment`
    FROM
      blog b
      LEFT JOIN blog_comment  bc
      ON b.id = bc.blog_id 
      GROUP BY b.id
    

    Active Record

    $this->db->select('b.*, COUNT(bc.blog_id) `comment`',FALSE);
    $this->db->from('blog b')
    $this->db->join('blog_comment bc','b.id = bc.blog_id ','LEFT');
    $this->db->group_by('b.id'); 
    $this->db->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?