dongpangzan6425 2018-01-21 12:45 采纳率: 100%
浏览 46
已采纳

基于codeigniter中另一个mysql表的post_id匹配获取会话user_id结果

I created a function to count total number of posts that based on 2 db tables below

1- posts:

    post_id user_id 
=========== ==== 
          61 122
          62 122
          96 122
          97 122
          98 122

2- post_meta:

   meta_id  post_id   status 
   ======== =======   ======
       1      61         1
       2      62         0
       3      62         1
       4      91         1
       5      97         1
       6      98         1

since user_id is exist in posts table whereas unique identifier on both table is post_id i am looking to count all post where status = 1 and user_id = $user_id here is my model function so far

/**
 * The function get_all_user_post_count gets all count posts for admin dashboard
 * 
 * @return array with posts or false 
 */
public function get_all_user_post_count($user_id) {
    $this->db->select('network_name,COUNT(meta_id) as number', false);
    $this->db->from('posts_meta');
    $this->db->join('posts', 'posts_meta.post_id=posts.post_id', 'left');
    $this->db->where(['status' => '1', 'user_id' => $user_id]);
    $this->db->group_by('network_name');
    $this->db->order_by('network_name');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        $result = $query->result();
        // Create new array
        $new_array = [];
        foreach ($result as $data) {
            $new_array[$data->network_name] = $data->number;
        }
        return $new_array;
    } else {
        return false;
    }
}

here is my controller function so far

// Count all sent posts per social network
        $count_sent_posts = $this->posts->get_all_user_post_count($this->user_id);
        $this->footer = [
            'statistics' => $statistics,
            'sent' => $count_sent_posts
        ];
        $this->user_layout();

A Database Error Occurred Error Number: 1052

Column 'status' in where clause is ambiguous

SELECT network_name, COUNT(meta_id) as number FROM posts_meta LEFT JOIN posts ON posts_meta.post_id=posts.post_id WHERE status = '1' AND user_id = '118' GROUP BY network_name ORDER BY network_name

Filename: models/Posts.php

Line Number: 393

  • 写回答

1条回答 默认 最新

  • dongzhenge2014 2018-01-22 17:32
    关注

    Try this one =>

    public function get_all_user_post_count($user_id) {
        $where = array('posts_meta.status ' => 1 , 'posts.user_id ' => $user_id);
        $this->db->select('posts_meta.network_name,COUNT(posts_meta.meta_id) as number', false);
        $this->db->from('posts_meta');
        $this->db->join('posts', 'posts_meta.post_id=posts.post_id', 'left');
        $this->db->where($where);
        $this->db->group_by('posts_meta.network_name');
        $this->db->order_by('posts_meta.network_name');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            $result = $query->result();
            // Create new array
            $new_array = [];
            foreach ($result as $data) {
                $new_array[$data->network_name] = $data->number;
            }
            return $new_array;
        } else {
            return false;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题